Preloading the Jupyter Notebook with specific classes/functions

前端 未结 1 1228
醉梦人生
醉梦人生 2021-01-01 07:58

I\'d like to preload a notebook with specific classes/functions that I\'ve defined in another file. More specifically, I\'d like to do this with python (something like loadi

相关标签:
1条回答
  • 2021-01-01 08:20

    When you start Jupyter notebook (or lab), if you have python script file(s) in

    location_of_ipython_profile/startup/
    

    then they will run on every IPython startup (to serve as standard ipython kernel). For example, on my machine I have a file named 00-first.py and it has simple content as follows:

    import numpy as np
    

    If there are other scripts, say, 50-middle.py and 99-last.py, they also run consecutively.

    When I open a new Jupyter notebook with ipython kernel. I will have many thing pre-executed according to those script files. For the first script, 00-first.py, it provides the symbol np to use right away. So, I can run print(np.pi) and get 3.141592... as output.

    To get the location_of_ipython_profile, one can run magic command on Jupyter notebook:

    !ipython locate profile
    

    In my case, I get C:\Users\swatc\.ipython\profile_default. To list all the files in the startup folder, I run:

    !dir C:\Users\swatc\.ipython\profile_default\startup\*.*
    

    Remember that there can be several python profiles on your computer. All the above is valid for the default profile.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题