Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one access the configuration from inside a IPython 3.x / Jupyter Notebook?

In particular I would like to know the base_url of the Notebook Server that the code is running in.

In IPython Notebooks version 2.x I used to do the following:

config = get_ipython().config
print config['NotebookApp']['base_url']

However this no longer works in IPython Notebook 3.x / Jupyter Notebooks.

EDIT: Some more detail on what I am trying to achieve.

I run various IPython Servers in separate Docker containers on the same host which are accessed through different base_urls. I would like to use the quantopian/qgrid package to display Pandas DataFrames inside the Notebook. Initially qgrid did not handle custom base_url prefixes for serving up a local copy of the Javascript dependencies but the code above allowed me to find the base_url in IPython 2 and to inject the relevant base_url into the Javascript template.

I would also like to use the mpld3 library in the Notebook and when browsing their documentation I found that they also mention that in "IPython 2.0+, local=True may fail if a url prefix is added (e.g. by setting NotebookApp.base_url)" so it seems that this is not an isolated problem and a good solution would be worthwhile.

Given @matt's comment below and thinking more about kernel vs frontend split, it makes sense that the NotebookApp config isn't accessible from the kernel. It's really the JS code that's generated that needs to know what the base_url is, so if someone can point me to where I can access this in the Notebook JS API, that should solve it.

like image 861
snth Avatar asked Jun 30 '15 09:06

snth


1 Answers

From the frontend side, if you publish javasscript, and assuming you are in a notebook (keep in mind that being in JS does not necessary mean notebook, you could be Atom-Hydrogen, or Jupyter-Sidecar) you can use a snipet like:

require(['base/js/utils'], function(utils){
    var base_url = utils.get_body_data('base-url')
})

The data-base-url attribute is set on the <body> tag of the notebook.

It is though not guarantied to stay this way. Usually, extension should be installed in the nbextensions folder, which should automatically resolve correctly:

require.config({

      ...
      paths: {
        nbextensions : '<base url>/nbextensions',
        kernelspecs : '<base url>/kernelspecs',
...
})

Nbextension is a search path, so if set correctly on the server, you shouldn't (most of the time) have to serve things yourself at custom URLs, nor to handle base_url yourself on frontend side.

like image 199
Matt Avatar answered Nov 06 '22 02:11

Matt