Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing passwords in Jupyter notebooks

Tags:

python

jupyter

Is there a way to store secrets/access secrets/passwords in notebooks? I have an api endpoint where I pull data from, and I dont want to expose the apiKey to everyone who can view the notebook.

like image 983
sargeMonkey Avatar asked Jul 06 '17 23:07

sargeMonkey


People also ask

Where is jupyter notebook password stored?

jupyter notebook password will prompt you for a password, and store the hashed password in your jupyter_notebook_config. json .

Can you store data in jupyter notebook?

Since the data in object storage is managed in terms of objects, they can store a high amount of information, which can be employed in Jupyter Notebooks to create constructive, high-quality projects.


2 Answers

The simplest solution I've been using for a while.

Use getpass Portable password input module.

import getpass

password = getpass.getpass('Enter your password')

print('Your password is: ' + password)
like image 145
Silver Ringvee Avatar answered Oct 02 '22 12:10

Silver Ringvee


cco's answer is good, but if you're looking for a simpler solution, many people use environmental variables to keep secrets segregated from source code.

For example, you can provide them when executing your script in the shell:

$ API_TOKEN=abc123 python script.py

In your source code:

import os
API_TOKEN = os.environ.get("API_TOKEN")

For your Jupyter notebooks, you can use python-dotenv or a similar package to "retrieve" a .env file that contains your project's secrets and is ignored by your version control system.

Once you've created your .env file (either manually, or using the package's command line tool), you can use python-dotenv in Jupyter (or IPython) like so:

%load_ext dotenv
%dotenv
import os
os.environ.get("API_TOKEN")
like image 42
Jacob Budin Avatar answered Oct 02 '22 13:10

Jacob Budin