Search code examples
pythonjupyter-notebookpathoperating-systemnbconvert

Python Jupyter notebook ossystem / nbconvert path change


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}')

Solution

  • 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.