Python Jupyter笔记本电脑ossystem / nbconvert路径更改
目前,我有带有报告的木星笔记本电脑,该报告希望将笔记本运行的输出(无代码)转换为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NBConvert似乎支持替代解决方案,您可以在其中将文件输出到-stdout 并将其对所需的文件夹进行了将类似的内容委托给所需的文件夹:
>
重定向将覆盖任何(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:
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.