Python Jupyter笔记本电脑ossystem / nbconvert路径更改

发布于 2025-02-11 10:47:40 字数 632 浏览 2 评论 0原文

目前,我有带有报告的木星笔记本电脑,该报告希望将笔记本运行的输出(无代码)转换为HTML格式。为了实现这一目标,我正在使用OS.System和一个Python脚本(请参阅Bellow)。但是,这将文件保存在我笔记本所在的同一文件夹中。我需要它将HTML报告保存在另一个目录中。关于如何仅在此报告的运行中修改输出文件的任何想法?

这是我的代码:

filename = 'ReportInUse'

today = datetime.today()
cdate = today.strftime("%d_%m_%Y")
report_name = f'{filename}_{cdate}.html'
cmd = f'jupyter nbconvert --execute {filename}.ipynb --no-input --to html'
os.system(cmd) 

# If report with date currently exists, remove
if os.path.exists(report_name):
  os.remove(report_name)

# Now renaming the base report to the current date
os.system(f'mv {filename}.html {report_name}')

Currently I have Jupiter notebooks with reports that I wish to convert into html format with the output from the notebook run (no codes). To achieve this I am using os.system and a python script (see bellow). However, this is saving the file in the same folder where my notebook is. I need it to save the html report in another directory. Any ideas on how can I modify the output file just for the run of this report?

This is my code:

filename = 'ReportInUse'

today = datetime.today()
cdate = today.strftime("%d_%m_%Y")
report_name = f'{filename}_{cdate}.html'
cmd = f'jupyter nbconvert --execute {filename}.ipynb --no-input --to html'
os.system(cmd) 

# If report with date currently exists, remove
if os.path.exists(report_name):
  os.remove(report_name)

# Now renaming the base report to the current date
os.system(f'mv {filename}.html {report_name}')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2025-02-18 10:47:40

NBConvert似乎支持替代解决方案,您可以在其中将文件输出到-stdout 并将其对所需的文件夹进行了将类似的内容委托给所需的文件夹:

cmd = f'jupyter nbconvert --execute {filename}.ipynb --no-input --to html --stdout > ./path/to/dir/{report_name}.html'
os.system(cmd)

>重定向将覆盖任何(and >>将附加到)与同一文件的现有文件在该位置的名称,因此您可能不需要第二个操作系统检查。

希望这会有所帮助。

nbconvert seems to support an alternative solution, where you might output your file to --stdout and redicted it to the desired folder something like so:

cmd = f'jupyter nbconvert --execute {filename}.ipynb --no-input --to html --stdout > ./path/to/dir/{report_name}.html'
os.system(cmd)

The > redirect would overwrite any (and >> would append to an) existing file with the same name in that location so you might not need that second os check later on.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文