Skip to content

How can I start a Jupyter server from Python #4873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mrocklin opened this issue Sep 10, 2019 · 7 comments
Open

How can I start a Jupyter server from Python #4873

mrocklin opened this issue Sep 10, 2019 · 7 comments

Comments

@mrocklin
Copy link

I have an existing Python process running a Tornado web application (Dask) and I would like to run a Jupyter notebook server from within this process, on the same event loop. I looked around and couldn't find any good documentation on how to start Jupyter from within Python. Is this easy?

Ideally I would do something like the following:

from notebook import Server

io_loop = tornado.ioloop.IOLoop.current()

server = Server(io_loop=io_loop)
await server.start()
@bollwyvl
Copy link
Contributor

bollwyvl commented Sep 10, 2019 via email

@mrocklin
Copy link
Author

That works! Nice.

OK, so if providing an event loop is hard, I imagine that it's also quite hard to provide my own, already running Tornado HTTPServer. True? It would be interesting to have only one of these running, assuming that Dask and Jupyter don't have any overlapping routes.

@bollwyvl
Copy link
Contributor

I think one could...

  • ... overload NotebookApp.init_webapp to inject an existing http_server...
    • init_webapp could be broken up to not conflate the initialization of NotebookApp.web_app and NotebookApp.http_server (which pretty much just owns the port and the SSL)
      • but the relationship has been 1:1 to date, as far as i can tell
  • ... and teach the originally-launched Application.__call__ how to (sometimes) dispatch to this new NotebookApp.__call__ (which i've never seen overload, but...)

i've usually been interested in taking the already-running, somewhat idiosyncratic notebook tornado loop/server/app to run other things in process so i can muck about with their workings in response to yet other things (usually, kernels, contents, and nb/lab extension-interfacing REST APIs). I must confess, dask has not been one of them, preferring to leave it on the other side of the proxy :P

@mrocklin
Copy link
Author

mrocklin commented Sep 11, 2019 via email

@oxbits
Copy link

oxbits commented Mar 19, 2025

In [1]: from notebook.notebookapp import NotebookApp
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from notebook.notebookapp import NotebookApp

ModuleNotFoundError: No module named 'notebook.notebookapp'

how do we do this today in 2025 ?

@oxbits
Copy link

oxbits commented Mar 19, 2025

found JupyterNotebookApp, looked promising, but no dice ....

In [2]: from notebook.app import JupyterNotebookApp

In [3]: app = JupyterNotebookApp()

In [4]: app.initialize([])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 app.initialize([])

File ~/_ws/_hax/djsci_hackz/env/lib/python3.13/site-packages/notebook/app.py:362, in JupyterNotebookApp.initialize(self, argv)
    360 def initialize(self, argv: list[str] | None = None) -> None:  # noqa: ARG002
    361     """Subclass because the ExtensionApp.initialize() method does not take arguments"""
--> 362     super().initialize()

File ~/_ws/_hax/djsci_hackz/env/lib/python3.13/site-packages/jupyter_server/extension/application.py:436, in ExtensionApp.initialize(self)
    434 self._prepare_config()
    435 self._prepare_templates()
--> 436 self._prepare_settings()
    437 self._prepare_handlers()

File ~/_ws/_hax/djsci_hackz/env/lib/python3.13/site-packages/jupyter_server/extension/application.py:304, in ExtensionApp._prepare_settings(self)
    302 # Make webapp settings accessible to initialize_settings method
    303 assert self.serverapp is not None
--> 304 webapp = self.serverapp.web_app
    305 self.settings.update(**webapp.settings)
    307 # Add static and template paths to settings.

AttributeError: 'ServerApp' object has no attribute 'web_app'

@bronlib
Copy link

bronlib commented Apr 26, 2025

The latest version has made it very easy to launch jupyter notebook from a python module:

  from notebook.app import main
  main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants