discovering which kernel has been started

强颜欢笑 提交于 2019-12-12 04:54:23

问题


Environment : I am spawning Notebook Servers (version 5) in Docker Containers, and have a system where users may select one of several Notebook kernels (thus, docker images) they wish to start.

I have a need to add an indicator to the notebook server page to indicate which particular kernel has been started.

I already have templates that modify the page.html and tree.html templates, so can add whatever I need into the html no problem.... what I'm failing to work out is how I get some piece of data from the Docker Container to the Notebook Server rendering code.

I'm deducing that it will probably be by adding something to the jupyter_notebook_config.py file - but can't find anything to help either way.

Has anyone done this?


回答1:


With thanks to the Jupyter development peoples... I have a solution:

Passing data in

In jupyter_notebook_config.py you can set a dict:

c.NotebookApp.jinja_template_vars = {'Ian_1':'One','ian_2':'Two'}

Reading data in the templates

In your Jinja template file, there's probably a block for getting parameters. In tree.html it looks like this:

{% block params %}
{{super()}}
data-base-url="{{base_url | urlencode}}"
data-notebook-path="{{notebook_path | urlencode}}"
data-terminals-available="{{terminals_available}}"
data-server-root="{{server_root}}"
{% endblock %}

All you need to do it add in your variables (obviously, they are case-sensitive).... eg:

{% block params %}
{{super()}}
data-base-url="{{base_url | urlencode}}"
data-notebook-path="{{notebook_path | urlencode}}"
data-terminals-available="{{terminals_available}}"
data-server-root="{{server_root}}"
ian_1="{{ian_1}}"
ian_2="{{ian_2}}"
{% endblock %}

(note the incorrect case-match for Ian_1)

if you now look at the source code of the /tree page in your notebook, you'll find them as attributes to the body tag:

<body class=""
     data-jupyter-api-token="<token-string>"
     data-base-url="/"
     data-notebook-path=""
     data-terminals-available="True"
     data-server-root="/home/jovyan"
     ian_1=""
     ian_2="Two"
     dir="ltr">

(and note that ian_1 is blank due to the case mis-match.)

Using variables

You can now use the variable as any other variable in Jinja (as per http://jinja.pocoo.org/)



来源:https://stackoverflow.com/questions/45710040/discovering-which-kernel-has-been-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!