timkpaine / jupyterlab_email

A jupyterlab extension to email notebooks directly from JupyterLab.
Apache License 2.0
59 stars 2 forks source link

Windows paths #86

Closed evyasonov closed 1 year ago

evyasonov commented 1 year ago

It's better to use os.path module (os.path.basename(), os.path.splitext()) when handing path:

msg = jupyterlab_email._email.make_email(
    r"C:\Users\me\Desktop\plots.ipynb",
    None,
    'asd'
)
File jupyterlab_email\_email.py:43, in make_email(path, model, from_, type, template, code, subject, header, footer, also_attach, also_attach_pdf_template, also_attach_html_template, postprocessor, postprocessor_kwargs, logger)
     12 def make_email(
     13     path,
     14     model,
   (...)
     27     logger=None,
     28 ):
     29     """
     30     path        : path to notebook
     31     model       : notebook itself (in case deployment strips outputs or
   (...)
     41     postprocessor : run postprocessor on soup
     42     """
---> 43     name = path.rsplit("/", 1)[1].rsplit(".", 1)[0]
     44     model = nbformat.writes(nbformat.reads(json.dumps(model), 4))
     46     logger = logger or logging

IndexError: list index out of range

Also, who do you use system executor in run() while you execute a notebook from the code directly

import nbformat
import nbconvert
import nbconvert.preprocessors

with open(file_name) as f:
    notebook = nbformat.read(f, as_version=4)
nbconvert.preprocessors.ClearOutputPreprocessor().preprocess(notebook)

error_output = None
try:
    nbconvert.preprocessors.ExecutePreprocessor(timeout=None).preprocess(notebook)
except nbconvert.preprocessors.CellExecutionError as e:
    error_output = str(e)

exporter = nbconvert.HTMLExporter(template_name='lab', embed_images=True, exclude_input = opts.mode == 'report')
(body, resources) = exporter.from_notebook_node(notebook)
with open(output_path, mode='w', encoding='utf-8') as f:
    f.write(body)

? No color conversion is needed (or at least you can use --colors=nocolor option), no regex, no subprocess -- looks much simpler