如何将HTML代码嵌入Jupyter笔记本输出?

3
我该如何使用R实现类似这个的效果?实际上,我想将Jupyter笔记本单元格的宽度设置为100%。在Python中,以下代码可以完美地工作:
from IPython.core.display import HTML, display
HTML("<style>.container { width: 100%; }</style>")

在R语言中有相应的功能吗?

谢谢!


最后,我编辑了/anaconda/Lib/site-packages/notebook/static/style/style.min.css文件,以始终“强制”100%的宽度。我在#notebook-container中添加了width: 100%; - ragesz
看起来你比使用R更好地解决了这个问题。感谢这个提示。你可能可以创建一个自定义的CSS文件,让Jupyter除了默认设置之外还能读取它。还有一些笔记本扩展可能会引起你的兴趣。 - Avi
2个回答

2
您可以使用display_html函数在 R 中显示 HTML 代码,例如:
// for full width
IRdisplay::display_html('<style>.container { width:100% !important; }</style>')

// this does not work with most sites due to same-origin policy, but no problem with local files   
IRdisplay::display_html('<iframe src="http://www.w3schools.com" width=1000, height=1000></iframe> ') 

参见:SecurityError: Blocked a frame with origin from accessing a cross-origin frame


1

将HTML代码和HTML表格嵌入IR Kernel Jupyter

  library(IRdisplay)

  display_html("your html code")

R中的一些包(如“knitr”)以HTML格式提供表格,因此如果您想将这些表格放入笔记本中:

library(knitr)
library(kableExtra)

dt <- mtcars[1:5, 1:6]
options(knitr.table.format = "html") 
html_table= kable(dt) %>%
   kable_styling("striped") %>%
   add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))

display_html(toString(html_table))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接