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.
jupyter notebook password will prompt you for a password, and store the hashed password in your jupyter_notebook_config. json .
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.
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)
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With