能够运行python的笔记本_Jupyter可以在Python笔记本中运行单独的R笔记本吗?

我不认为你可以使用 %run magic命令,因为它在当前内核中执行文件 .

Nbconvert有一个执行API,允许您执行笔记本 . 因此,您可以创建一个shell脚本来执行所有笔记本,如下所示:

#!/bin/bash

jupyter nbconvert --to notebook --execute 1_py3.ipynb

jupyter nbconvert --to notebook --execute 2_py3.ipynb

jupyter nbconvert --to notebook --execute 3_py3.ipynb

jupyter nbconvert --to notebook --execute 4_R.ipynb

由于您的笔记本电脑不需要共享状态,因此这应该没问题 . 或者,如果您真的想在笔记本中执行此操作,则可以使用execute Python API从笔记本中调用nbconvert .

import nbformat

from nbconvert.preprocessors import ExecutePreprocessor

with open("1_py3.ipynb") as f1, open("2_py3.ipynb") as f2, open("3_py3.ipynb") as f3, open("4_R.ipynb") as f4:

nb1 = nbformat.read(f1, as_version=4)

nb2 = nbformat.read(f2, as_version=4)

nb3 = nbformat.read(f3, as_version=4)

nb4 = nbformat.read(f4, as_version=4)

ep_python = ExecutePreprocessor(timeout=600, kernel_name='python3')

#Use jupyter kernelspec list to find out what the kernel is called on your system

ep_R = ExecutePreprocessor(timeout=600, kernel_name='ir')

# path specifies which folder to execute the notebooks in, so set it to the one that you need so your file path references are correct

ep_python.preprocess(nb1, {'metadata': {'path': 'notebooks/'}})

ep_python.preprocess(nb2, {'metadata': {'path': 'notebooks/'}})

ep_python.preprocess(nb3, {'metadata': {'path': 'notebooks/'}})

ep_R.preprocess(nb4, {'metadata': {'path': 'notebooks/'}})

with open("1_py3.ipynb", "wt") as f1, open("2_py3.ipynb", "wt") as f2, open("3_py3.ipynb", "wt") as f3, open("4_R.ipynb", "wt") as f4:

nbformat.write(nb1, f1)

nbformat.write(nb2, f2)

nbformat.write(nb3, f3)

nbformat.write(nb4, f4)

请注意,这几乎只是从nbconvert执行API文档中复制的示例:link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值