How to access configuration from inside IPython 3.x / Jupyter Notebook?

In particular, I would like to know the base_url the Notebook server that the code is working.

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

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

However, this no longer works on IPython Notebook 3.x / Jupyter laptops.

EDIT: Some more details on what I'm trying to achieve.

I am running various IPython servers in separate Docker containers on the same host, accessed through different base_url s. I would like to use the quantopian / qgrid package to display Pandas DataFrames inside a laptop. Initially, qgrid did not handle qgrid user prefixes to serve a local copy of Javascript dependencies, but the code above allowed me to find base_url in IPython 2 and insert the corresponding base_url into the Javascript template.

I would also like to use the mpld3 library in a laptop, and when I looked at their documentation I found that they also mention that in "IPython 2.0+, local = True may fail if the url prefix is ​​added (for example, by setting NotebookApp.base_url) " therefore it seems that this is not an isolated problem and a good solution would be helpful.

Given the @matt comment below and thinking more about the separation of the kernel and the interface, it makes sense that the NotebookApp configuration is not accessible from the kernel. This is really JS code that should know what base_url , so if someone can tell me where I can access this in the JS API for laptops, this should solve it.

+6
source share
2 answers

From the outside, if you publish javasscript and think that you are in a laptop (keep in mind that JS does not need an average laptop, you can be Atom-Hydrogen or Jupyter-Sidecar), you can use a sniper like:

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

The data-base-url attribute is set in the <body> laptop.

However, this is not guaranteed. Typically, the 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 you have configured the server correctly, you should not (most of the time) service yourself on custom URLs, and also do not process base_url yourself on the interface side.

+3
source

After pretty much delving into internal IPython, 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 now I understand that the kernel is not really the right place to get this information. I will probably delete this answer as soon as better answers appear.

-1
source

Source: https://habr.com/ru/post/990001/


All Articles