start ipython notebook with python file

后端 未结 4 1208
既然无缘
既然无缘 2021-02-04 20:09

I am not very familiar with python/ipython but somebody was asking me whether it is possible to start an ipython notebook with a specific python file. It then could be used for

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 20:58

    If you're talking about launching an iPython notebook server via Python, I use this:

    #!/usr/bin/env python
    from IPython.terminal.ipapp import launch_new_instance
    from IPython.lib import passwd
    from socket import gethostname
    import warnings
    warnings.filterwarnings("ignore", module = "zmq.*")
    sys.argv.append("notebook")
    sys.argv.append("--IPKernelApp.pylab='inline'")
    sys.argv.append("--NotebookApp.ip=" + gethostname())
    sys.argv.append("--NotebookApp.open_browser=False")
    sys.argv.append("--NotebookApp.password=" + passwd())
    launch_new_instance()
    

    Obviously you can change the arguments if you so desire.

    At my work we have one use case that does what you're saying--automatically generates a python file, then loads a new ipython server for the user to access it. However, it's a pretty special use case--for normal debugging, I would recommend just starting in iPython and not making your *.py file until the bugs are gone.

    OR

    If you're talking about actually automatically navigating to the page that corresponds to a python file made available by an ipython notebook server, then (1) make sure you're using ipython 2, and (2) figure out what you're desired url is (it should be deterministic) and (3) use the webbrowser module to navigate to that url.

提交回复
热议问题