Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto configure Jupyter password from command line

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.

like image 436
Charan Avatar asked Nov 03 '17 09:11

Charan


2 Answers

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.

like image 136
Marcelo Tournier Avatar answered Sep 22 '22 03:09

Marcelo Tournier


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
like image 39
savfod Avatar answered Sep 26 '22 03:09

savfod