[Python] Use Jupyter notebooks as function calls

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    How to invoke a *.ipynb file as if it was a generic python function accepting an input and producing an output?

    For example, graph_parser.ipynb is a Jupyter notebook, it needs as input some file path (e.g. .\location\big_graph.txt) and produces as output some pyhton object. Immagine a generic python script, for example running on a web-server, calling the notebook with notebook_result = run_notebook("local/path/to/graph_parser.ipynb", ".\location\big_graph.txt") and getting the pyhton object produced by the notebook assigned to notebook_result.

    How would you go about achieving this? How to invoke a *.ipynb file, passing in some arguments and getting the output of the computation out? I know the tool nbconvert provides a python API to execute a *.ipynb file programmatically so I suppose one way would be to hardcode in the *.ipynb file an input and an output folder but I am looking for a more flexible apporach.

    Motivation


    I would like to use Jupyter Notebooks as function calls because they offer a fast way of visualising and improving processing steps in complex pipelines. In a project I was involve we ended duplicating the code of a notebook for converting an edge list into a complex python object. We needed the notenook because it was useful for experimenting but we also needed a standalone python function to parse graphs server-side.

    Continue reading...

Compartilhe esta Página