jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.63k stars 4.89k forks source link

How can I start a Jupyter server from Python #4873

Open mrocklin opened 5 years ago

mrocklin commented 5 years ago

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 commented 5 years ago

Very close!

from notebook.notebookapp import NotebookApp

app = NotebookApp()

app.initialize([])

Square brackets are the command line arguments. Might want to set port and token, if you actually want to talk to it.

I've never given it a custom io_loop, though... Probably have to overload .start, as it always use the current one.

More:

https://gist.github.com/bollwyvl/bd56b58ba0a078534272043327c52bd1

On Tue, Sep 10, 2019, 13:27 Matthew Rocklin notifications@github.com wrote:

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()

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/notebook/issues/4873?email_source=notifications&email_token=AAALCRHIHL3ZSSM5VAMXR5DQI7KJTA5CNFSM4IVKQCT2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HKQKPZA, or mute the thread https://github.com/notifications/unsubscribe-auth/AAALCRCWQ7HVOUGTH3PYDWDQI7KJTANCNFSM4IVKQCTQ .

mrocklin commented 5 years ago

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 commented 5 years ago

I think one could...

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 commented 5 years ago

It's always better to keep out the riff raff :)

On Tue, Sep 10, 2019, 5:35 PM Nicholas Bollweg notifications@github.com wrote:

I think one could...

i've usually been interested in taking the already-running, somewhat idiosyncratic https://github.com/jupyter/notebook/blob/6.0.1/notebook/notebookapp.py#L47 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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jupyter/notebook/issues/4873?email_source=notifications&email_token=AACKZTCZ7UZGCVYBFCN7JVTQJA4M7A5CNFSM4IVKQCT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6M4LVI#issuecomment-530171349, or mute the thread https://github.com/notifications/unsubscribe-auth/AACKZTGQPSRQFBQPSUZN4MTQJA4M7ANCNFSM4IVKQCTQ .