Reusing code from different IPython notebooks

前端 未结 10 1211
南方客
南方客 2020-12-13 08:52

I am using IPython and want to run functions from one notebook from another (without cutting and pasting them between different notebooks). Is this possible and reasonably e

10条回答
  •  情歌与酒
    2020-12-13 09:16

    There is also a "write and execute" extension, which will let you write the content of a cell to a file (and replace old content -> update code), which can then be imported in another notebook.

    https://github.com/minrk/ipython_extensions#write-and-execute

    In one notebook (two cells)

    %reload_ext writeandexecute
    --
    %%writeandexecute -i some_unique_string functions.py
    def do_something(txt):
        print(txt)
    

    And then in the other notebook:

    from functions import do_something
    do_something("hello world")
    

提交回复
热议问题