如何使用笔记本Jupyter在html中发布R代码 [英] How publish R code in html using notebook jupyter

查看:126
本文介绍了如何使用笔记本Jupyter在html中发布R代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多人都在谈论使用jupyter笔记本将代码转换和共享为HTML,就像是小菜一碟,但这已经成为我的噩梦!

I see many people talk about converting and sharing codes to HTML using jupyter notebooks like it is a piece of cake but it has turned into a nightmare for me!

我确实可以通过使用下拉菜单下载代码来将代码转换为html,但只能在我自己的计算机上访问,如果将其作为链接发送给其他人,他们将看到:无法访问此站点". HTML下载仅提供本地主机上的链接,并且不可共享.有人知道它的处理方式吗?我确实使用了github,但这似乎仅适用于Jupyter Python,而不是R.

I can sure convert my codes into html by downloading them using the dropdown menu but it is only accessible on my own machine and if I send it as a link to someone else, they will see :this site cant be reached". The HTML download only gives a link on local host and it is not shareable. Does anyone know what is the way around it? I did use github but that seems only for Jupyter Python and not R does.

这里有帮助吗?

推荐答案

最后提到使用Github的方法.您可以使用那个或Github的Gist网站直接以.ipynb形式(实际上是json编码)共享笔记本,并且您发送链接的任何人都可以将其视为静态呈现.您当然可以发布到html,但是随后您需要托管html或使用 githack 链接到它.始终可以选择转为PDF并通过电子邮件发送.既然您谈论转换,我假设您不需要它就处于活动状态.但是,也可以使用MyBinder.org,请参见此处的示例,您可以在其中单击顶部的启动"按钮,然后旋转以前其他人制作的笔记本.

Way at the end you mention using Github. You can share the notebook directly in it's .ipynb form (actually json encoded) using that or Github's Gist site and anyone you send a link can see it as nicely rendered static. You can of course publish to html, but then you need to host html or use githack to link to it. There is always the option of going to PDF and sending that via email. Since you talk about converting I assume you don't need it to be active; however, that is possible too using MyBinder.org, see an example here where you can click the top 'launch' button and spin up a notebook someone else previously made.

我将逐一详细说明所有这些建议.

I'll step through each of those suggestions with further clarification.

如果笔记本是公开的,则可以使用Github共享笔记本.想象一下,您进行了公共回购,并以.ipynb文件格式在其中放置了一个笔记本. (注意:所有这些步骤都可以直接在Github的Web界面中完成,而无需在本地计算机上使用git.)让我们使用此处.)笔记本可能会渲染否则可能会尝试超时. Github的渲染效果并不完美,因此请不要依赖它.更好的方法是使用nbviewer网站.要使用nbviewer网站,您需要从地址栏中复制笔记本的URL.对于此示例,您将复制:

You can use Github to share a notebook if it is public. Imagine you made a public repo and put a notebook there in its .ipynb file form. (Note: all those steps can be done directly in Github's web interface without you needing git on your local computer.) Let's use here as a theoretical example. If you go to that repo you'll see a notebook index.ipynb listed. Click on the link to directly view it. (You should now be here.) The notebook may render or it may try and timeout. Github's rendering isn't perfect so don't rely on it. Better is to use the nbviewer site. And to use the nbviewer site, you'll want to copy the URL of the notebook from your address bar. For this example, you'd copy:

https://github.com/binder-examples/r/blob/master/index.ipynb

在剪贴板中使用该URL,请访问nbviewer网站,网址为 https://nbviewer.jupyter.org/.转到nbviewer的首页时,将URL粘贴在输入Jupyter Notebook的位置..."下方顶部的表单中,然后单击Go!.这将带您进入没有任何Github页眉/草皮呈现的笔记本的漂亮页面.请注意,笔记本中是Python代码还是R代码都没有关系.在nbviewer中看起来会很好.要与某人共享此视图,请复制此页面地址栏中的URL.在这种情况下,它将是:

With that URL in your clipboard, go to the nbviewer site at https://nbviewer.jupyter.org/ . When you get to nbviewer's front page, paste the URL in the form at the top under 'Enter the location of a Jupyter Notebook...' and click Go!. That will take you to a nice page of the notebook rendered without any Github header/cruft. Note that it doesn't matter if it is Python code or R code in the notebook. It will look nice at nbviewer. To share this view with someone copy the URL from the address bar of this page. In this case it will be:

https://nbviewer.jupyter.org/github/binder-examples/r/blob/master/index.ipynb

您现在可以与任何人共享该链接.

You can share that link with anyone now.

该链接告诉nbviewer网站从Github获取笔记本代码并进行渲染.如果以后进行更新,他们仍然可以使用相同的链接来打开更新的表单. (请注意,nbviewer有时可能需要10分钟左右才能从Github收集更新.)还可以使用Github的URLS和nbviewer共享笔记本的特定版本.您想了解如何在Github上浏览特定提交的文件,并使用这些URL.

That link tells the nbviewer site to fetch the notebook code from Github and render it. If you update it later, they can still use the same link and get the updated form opening. (Note that nbviewer can sometimes take 10 or so minutes before it collects updates from Github.) It is possible to also share a specific version of a notebook using Github's URLS and nbviewer. You'd want to learn about how browse the files of specific commits at Github to do that and use those URLs.

需要注意的是,nbviewer渲染页面的链接遵循引用Github存储库的模式,因此无需进行所有这些步骤即可建立链接.

Something to note is that the link for the nbviewer rendering page follows a pattern referring to the Github repo, and so it is possible to make the link without going through all those steps.

要注意的另一件事是,在下载按钮的左侧,右上角有一个带有三个环的符号.如果单击这些环,则可以通过MyBinder.org启动该笔记本的活动版本.当会话启动时,该笔记本将运行,而无需安装任何其他软件包或任何其他操作如果配置了存储库,则基础技术可以识别配置文件并构建适当的环境.如果未在存储库中考虑到Binder的设置,则将其设置为笔记本,但是由于默认情况下环境并不完全兼容,因此在尝试运行时可能会出错.

Another thing to note is that there is a symbol with three rings in the upper right corner, just to the left of the download button. If you click on those rings you can launch an active version of that notebook via MyBinder.org. When the session spins up, that notebook will run without need for installing any additional packages or anything if the repository was configured so the underlying tech has recognized the configuration files and built the proper environment. It would still open the notebook if the repository wasn't set up with Binder in mind, but you may get errors when you try to run as the environment isn't completely compatible by default.

此过程与在GitHub和nbviewer共享笔记本"下所述的过程非常相似.关于将笔记本.ipynb文件放置在何处的前几个步骤有所不同.您可以在 https://gist.github.com/上访问Github的Gist网站.该站点仅用于共享代码段或仅几个文件,并且不具有完整的Github存储库所具有的所有功能.但是,它非常适合您使用.您可以粘贴.ipynb文件的内容或将其上传到此处,然后按照nbviewer上的URL进行使用,如"GitHub和nbviewer共享笔记本"中所述.

This process is very similar to the one described under 'GitHub and nbviewer to share a notebook'. The first few steps about where you put the notebook .ipynb file differ. You'd go to Github's Gist site at https://gist.github.com/ . This site is meant for sharing snippets of code or just a few files and doesn't have all the features a full Github repository has. However, it is great for your uses. You can paste the contents of the .ipynb file or upload it there and then use the URL over at nbviewer as described for described under 'GitHub and nbviewer to share a notebook'.

此过程与"Gist网站和nbviewer共享笔记本"下所述的过程非常相似.但是,您上传到Gist网站的内容和用于共享渲染的技术有所不同.这次,您需要将您提到的HTML文件放在Gist网站上.此时,它看起来像HTML代码,而不是笔记本.将Gist网站上的页面URL复制到剪贴板.转到raw.githack.com网站,网址为 http://raw.githack.com/.将URL粘贴在上面说要粘贴URL的顶部的表格行中.现在,您将转到呈现HTML的页面.您可以从地址栏中复制该URL并与任何人共享,类似于使用nbviewer查看笔记本的方式.

This process is very similar to the one described under 'Gist site and nbviewer to share a notebook'. However, what you upload to the Gist site and the tech you use to share a rendering differ. This time you'll want to put the HTML file you mentioned at the Gist site. It will look like HTML code and not a notebook at this point. Copy the URL of the page at the Gist site to your clipboard. Go to the raw.githack.com site at http://raw.githack.com/ . Paste the URL in the form line at the top where it says to paste a URL. You'll now go to a page that is the rendering of the HTML. You can copy the URL from your address bar and share that with anyone, similar to how you use nbviewer to view a notebook.

这与"Gist网站和githack共享笔记本的HTML"下的描述非常相似.区别在于您将使用实际的Github存储库来存储笔记本的HTMl版本.

This is very similar to what is described under 'Gist site and githack to share a notebook's HTML'. The difference is you'd use an actual Github repo to store the HTMl version of the notebook.

您没有提到这条路线.因此,我将简要介绍一下.根据您安装的Jupyter或使用的位置,我发现保存PDF表单不一致. 此处.如果您去那里并按launch binder按钮,则可以启动一个可进行PDF保存的会话.您需要专门使用Export to PDF图标,该图标将出现在笔记本上方的菜单栏中.

You didn't mention this route. So I'll keep it brief. Depending on your installation of Jupyter or where you are using it, I've seen saving a PDF form be inconsistent. One place it should be possible, is here. If you go there and press the launch binder button, you can spin up a session where PDF saving works. You'll want to specifically use the Export to PDF icon that will appear on the too menubar just above the notebook.

我也会保持简短,因为上面部分以各种形式对其进行了介绍,并且比使用nbviewer更高级.

I'll also keep this brief since it was touched on in various forms in the sections above and is more advanced than using nbviewer.

"GitHub和nbviewer共享笔记本"部分的末尾描述了实现此目的的一种方法.您也可以不使用nbviewer来执行此操作.让我们回到示例存储库此处.从该页面的顶部复制URL:

One route to doing this was described at the end of the 'GitHub and nbviewer to share a notebook' section. You can do this without using nbviewer as well. Let's go back to the example repo here. Copy the URL form the top of that page:

https://github.com/binder-examples/r

现在转到位于 https://mybinder.org/的MyBinder.org网站,然后将该URL粘贴到在表单顶部显示"GitHub存储库名称或URL",然后单击启动.这将使用由该存储库中看到的特殊文件配置的环境启动一个临时会话.您将可以单击笔记本index.ipynb并运行它.切勿共享正在运行的会话链接,因为它是临时的.

Now go to the MyBinder.org site at https://mybinder.org/ and paste that URL into the top of the form where it says 'GitHub repository name or URL' and click launch. This will spin up a temporary session using the environment configured by special files it has seen in that repository. You'll be able to click on the notebook index.ipynb and run it. Never share the running session link as it is temporary.

要分享一些东西,以便其他人可以在您的笔记本上发起自己的会话,您需要在 https://github.com上看到的那样/binder-examples/r .

To get something to share so that others can launch there own session with your notebook, you'll want to explore using the options for form more at https://mybinder.org/ to generate links you can share with others. For example, you can add in a path to a specific notebook so when the session launches it starts with that notebook already opened. Once you have filled in the form you want to copy for sharing the URL that you'll see generated under 'Copy the URL below and share your Binder with others:'. You'll see that form also allows making code for clickable badges like you see at https://github.com/binder-examples/r .

请注意,启动与可以运行的笔记本的会话是可行的,因为该存储库已配置为安装irkernel.没有任何配置,使用R只能从任何存储库中仅运行任何笔记本. (默认情况下将仅具有Python内核.)但是,我一直在引用的示例存储库可以用作模板,使用Use this template按钮 Holepunch 可以帮助您完成类似的步骤,以共享基于R的代码.

Note the launching of the session with the notebook that can be run works because that repository was configured to install the irkernel. It won't work to run just any notebook from any repository with R without some configuration. (It will default to only having Python kernels.) However, the example repository I have been referencing can be used as a template to make a new repository of your own using the Use this template button here to ease the steps needed to do this. There is also Holepunch to help in similar steps for sharing R-based code.

您还可以向人们发送笔记本.ipynb文件,然后他们可以选择.他们可以使用 nbpreview ,它允许您上传本地笔记本.ipynb文件并从当地来源.如果它在笔记本中对工作敏感,并且您担心要上传,则还可以在本地安装和运行nbpreview,请参见这里.也可以在本地安装nbviewer并查看它.或者他们可以运行您使用Jupyter系统发送的笔记本文件.由于其中某些选项的元素依赖于高级技能并知道如何安装,因此它们可能并不适合所有相关人员.

You could also send people the notebook .ipynb file and then they have some options. They can use nbpreview which allows you to upload a local notebook .ipynb file and render it from a local source. If it sensitive work in the notebook and you are concerned about uploading, it is also possible to install and run nbpreview locally, see here. It is also possibe to install nbviewer locally and view it. Or they can run the notebook file you sent using their Jupyter system. Because elements of some of those options relies advanced skills and knowing how to install things, they may not be best for all involved.

规避这些障碍的一种方法是建立一个能够通过MyBinder服务的存储库,该存储库完全适合运行私人笔记本,然后建议用户如何启动会话并分别上传您与他们共享的笔记本,例如通过电子邮件.您可以在此处了解更多有关MyBinder.org上的安全性.

One way to circumvent those hurdles is to set up a repository capable of serving via MyBinder an environment fully suited for running the private notebook and then advise the user how to spin up a session and upload the notebook that you shared with them separately, such as via email. You can see here for more on security on MyBinder.org.

随着笔记本电脑的普及,使用和共享笔记本电脑的方式也越来越多.这个答案只涉及到一些更开放,公开的方式.当您添加具有授权的Jupyter Hubs时,还有更多内容,因为您可以在JupyterHub网络中共享指向自己笔记本的链接.现在也有商业产品.您总是可以使用Google Colab或Azure笔记本来共享它们.

As notebooks become more popular, there becomes more ways to use and share them. This answer only touched on some of the more open, public ways. When you add in Jupyter Hubs with authorization there are still more because you can share a link to your own notebook in the JupyterHub network. Also there are commercial offerings now. You could always use Google Colab or Azure notebooks to share them as well.

这篇关于如何使用笔记本Jupyter在html中发布R代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆