Reputation: 5647
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_url
s. 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.
Upvotes: 6
Views: 2301
Reputation: 27843
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.
Upvotes: 3
Reputation: 5647
After quite a lot of digging into IPython internals I found something that works for me:
from IPython.config.loader import load_pyconfig_files
config = get_ipython().config
profiledir = config['ProfileDir']['location']
nbconfig = load_pyconfig_files(['ipython_notebook_config.py'], profiledir)
print nbconfig['NotebookApp']['base_url']
EDIT: This works on my installation but I understand now that the kernel is not really the right place to get this info. I'll probably delete this answer once some better answers are up.
Upvotes: -1