Auto configure Jupyter password from command line

2019-08-12 16:26发布

I learnt that Jupyter-notebook can be configured with a password instead of token.

Two steps-

$ jupyter notebook password
Enter password:  ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json

I want to automate this process (in Dockerfile, I won't be able to enter manually when prompted for password), something like this,

echo 'password' | jupyter notebook password

This should auto input my 'password' to the shell when prompted to Enter password and Verify password

Can you give me a shell command which can automate this password setup without user intervention.

4条回答
Emotional °昔
2楼-- · 2019-08-12 16:38

I managed to solve this issue with this commit. I created a custom python file that reuses part of the notebook passwd() function and then stores it in the ~/.jupyter/jupyter_notebook_config.py file. In my case I'm using a docker-compose so passing the password to be saved was a bit more complex, but you can pass it as an environment file easily with Docker:

# -*- encoding: utf-8 -*-

import os
import fileinput
import hashlib
import random
from ipython_genutils.py3compat import cast_bytes, str_to_bytes

# Get the password from the environment
password_environment_variable = os.environ.get('JUPYTER_PASSWORD')

# Hash the password, this is taken from https://github.com/jupyter/notebook/blob/master/notebook/auth/security.py
salt_len = 12
algorithm = 'sha1'
h = hashlib.new(algorithm)
salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
h.update(cast_bytes(password_environment_variable, 'utf-8') + str_to_bytes(salt, 'ascii'))
password = ':'.join((algorithm, salt, h.hexdigest()))

# Store the password in the configuration
setup_line = "#c.NotebookApp.password = ''"
new_setup_line = setup_line.replace("''", "u'" + password + "'")
new_setup_line = new_setup_line.replace("#", "")
setup_file = os.getenv("HOME") + "/.jupyter/jupyter_notebook_config.py"

for line in fileinput.input(setup_file, inplace=True):
    print line.replace(setup_line, new_setup_line),

for line in fileinput.input(setup_file, inplace=True):
    print line.replace("#c.NotebookApp.password_required = False", "c.NotebookApp.password_required = True"),
查看更多
我只想做你的唯一
3楼-- · 2019-08-12 16:46

1. Generate hash with passwd function

Generate hash for your_password manually in console:

In [1]: from notebook.auth import passwd; passwd()
Enter password: *************
Verify password: *************
Out[1]: 'sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'

or in script:

$ python3 -c "from notebook.auth import passwd; print(passwd('your_password'))"
sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea

2. Run jupyter with NotebookApp.password param

When started:

jupyter notebook --NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'

or via jupyter config, e.g. in Dockerfile as in this answer:

RUN jupyter notebook --generate-config
RUN echo "c.NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'">>/root/.jupyter/jupyter_notebook_config.py
查看更多
乱世女痞
4楼-- · 2019-08-12 16:48

To setup a password from bash using a echo, you can do the following

jupyter notebook --NotebookApp.password="$(echo hello | python -c 'from notebook.auth import passwd;print(passwd(input()))')"

Please notice that sending password over an echo is highly insecure.

查看更多
时光不老,我们不散
5楼-- · 2019-08-12 16:54

You can provide the token argument as a workaround for a password to jupyter in Docker:

sudo docker run -i -t -p 8888:8888 -v /home/YOUR_USER:/home/ -e USERID=1003 continuumio/anaconda3 /opt/conda/bin/jupyter lab --ip='*' --port=8888 --no-browser --NotebookApp.token='YOUR_PASSWORD' --allow-root --notebook-dir=/home/

Then, enter in your address:8888 (without the token) and, when prompted, enter your token instead of your password.

Don't forget to check your USERID (with the id command in terminal), to ensure you will be able to read+write in sync with folders outside your container.

查看更多
登录 后发表回答