Best practices for turning jupyter notebooks into python scripts

后端 未结 4 1450
误落风尘
误落风尘 2021-01-29 19:19

Jupyter (iPython) notebook is deservedly known as a good tool for prototyping the code and doing all kinds of machine learning stuff interactively. But when I use it, I inevitab

4条回答
  •  心在旅途
    2021-01-29 20:07

    I made a module recently (NotebookScripter) to help address this issue. It allows you to invoke a jupyter notebook via a function call. Its as simple to use as

    from NotebookScripter import run_notebook
    run_notebook("./path/to/Notebook.ipynb", some_param="Provided Exteranlly")
    

    Keyword parameters can be passed to the function call. Its easy to adapt a notebook to be parameterizable externally.

    Within a .ipynb cell

    from NotebookScripter import receive_parameter
    
    some_param = receive_parameter(some_param="Return's this value by default when matching keyword not provided by external caller")
    
    print("some_param={0} within the invocation".format(some_param))
    

    run_notebook() supports .ipynb files or .py files -- allowing one to easily use .py files as might be generated by nbconvert of vscode's ipython. You can keep your code organized in a way that makes sense for interactive use, and also reuse/customize it externally when needed.

提交回复
热议问题