Skip to main content
Welcome to the first demo lesson. In this guide you’ll set up a local development environment for working with Jupyter Notebook and GitHub, then securely store an OpenAI API key for use in your notebooks. Jupyter Notebook is ideal for step-by-step prototyping, visualization, and interactive documentation — particularly useful for data science, machine learning, and teaching. Pairing Jupyter with GitHub gives you version control, collaboration, and cloud backup so your notebooks remain reproducible and shareable. This walkthrough covers:
  • Installing Anaconda to run Jupyter
  • Launching and using Jupyter Notebook (kernels, running cells, common pitfalls)
  • Creating a secure .env file for your API key and loading it in Python
  • Creating a GitHub repository and committing your project safely

1) Install Anaconda (to run Jupyter)

  1. Visit the Anaconda distribution page: https://www.anaconda.com/products/distribution
  2. Download the free distribution for your operating system.
  3. You can skip account registration and proceed with the installer.
  4. After installation, launch Anaconda Navigator to access Jupyter Notebook and other tools.
The image shows the Anaconda Navigator interface with various applications listed, such as PyCharm and JupyterLab, displaying options to install or launch them.

2) Launching Jupyter Notebook

  • Start Jupyter Notebook from Anaconda Navigator (or run jupyter notebook from a terminal). It opens in your default browser at a local URL such as http://localhost:8888/tree.
  • Create a project folder: click New → New Folder, rename it (e.g., Demo Project) and open it.
  • Create a new notebook inside the folder: New → Python 3 (or an available kernel).
  • Rename the notebook by clicking the title (e.g., change “Untitled” to Demo).
The menu bar contains File, Edit, View, Run, Kernel, Settings, Help. The toolbar provides quick actions: run cell, move cells, cut/copy/paste, restart kernel, etc.
The image shows a Jupyter notebook interface with the "Run" menu open, highlighting the "Run Selected Cell" option.

Kernel & run controls — quick reference

ActionWhat it does
InterruptStops currently executing code (useful for infinite loops or long-running operations).
RestartRestarts the kernel and clears in-memory state (variables, imports).
ShutdownEnds the kernel session.
Run cell (▶)Executes the current cell and advances depending on the option chosen.
Tip: If your notebook behaves unexpectedly (old variables, mismatched outputs), use Kernel → Restart & Clear Output to get a clean runtime and reproduce results deterministically.

3) Examples — running cells and managing the kernel

Infinite loop example (run with caution):
while True:
    print("Hi")
If you execute this, it will continually print until you interrupt the kernel (Kernel → Interrupt or the stop button). Common error example (Python is case-sensitive):
while true:
    print("hi")
This raises a NameError because true (lowercase) is not defined in Python. The correct boolean literal is True.

Cell execution order and kernel state

Cells execute in the kernel’s current state. If you modify a later cell but do not re-run it, the kernel will still use the previously executed value. Example:
foo = 1 + 1
print(foo)  # outputs: 2
If you later change another cell to:
foo = 1 + 1 + 1
# (but do not run this cell)
and then re-run the print(foo) cell without running the updated assignment cell, the output remains 2. To update the kernel state, run the assignment cell or restart and re-run the notebook in order. Restarting the kernel (Kernel → Restart & Clear Output) clears state and outputs and is useful to confirm reproducibility.

4) Store a secure .env file for your API key

Keeping secrets outside source control is essential. Create a .env file locally and never commit it. Create .env programmatically (replace YOUR_OPENAI_API_KEY with your real key after you obtain it):
with open(".env", "w") as f:
    f.write("OPENAI_API_KEY=YOUR_OPENAI_API_KEY")
Add .env to .gitignore before committing:
.env
Example .env content (do not paste real keys into shared or public files):
OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Do not commit .env or your API keys to GitHub. Keep secrets out of version control and use environment-specific secret management in production.

5) Getting your OpenAI API key

  1. Go to the OpenAI dashboard: https://platform.openai.com/account/api-keys
  2. Log in or create an account.
  3. From the dashboard, open Settings (cogwheel) → API keys.
  4. Create a new secret key, give it a descriptive name (e.g., Demo API Key final), and copy it immediately — the secret is shown only once.
The image shows an API key management interface on the OpenAI platform, displaying a "Save your key" pop-up with an API key and instructions. The background lists several API keys with options to edit or delete them.
After creating the key, paste it into your .env file (or update the file manually).

6) Loading environment variables in Python

Install python-dotenv if not already installed:
pip install python-dotenv
Load the .env and verify the key is present:
from dotenv import load_dotenv
import os

load_dotenv()
print(os.getenv("OPENAI_API_KEY") is not None)  # Should print: True

7) Add a small code test to the notebook

Add a few simple cells to confirm everything runs:
print(1)
print("Hello World")

8) Set up a GitHub repository and commit safely

  1. Sign in to GitHub: https://github.com
  2. Create a new repository (e.g., Demo-API-Setup). Choose private if you prefer and add a README.
The image shows a GitHub interface for creating a new repository. It features fields for the repository name, description, visibility options, and additional setup choices like adding a README file.
Confirm .gitignore includes .env before committing. Quick Git commands (run from your project root):
CommandPurpose
git initInitialize a new repository
git add .Stage all files (ensure .gitignore is set first)
git commit -m "Initial commit"Create the first commit
git branch -M mainRename the default branch to main
git remote add origin https://github.com/your-username/Demo-API-Setup.gitAdd your remote (replace with your URL)
git push -u origin mainPush commits to GitHub and set upstream
Replace the remote URL with your repository’s HTTPS or SSH URL.

Wrap-up

You have completed the essential setup:
  • Installed Anaconda and launched Jupyter Notebook.
  • Learned to manage kernels and run cells reliably.
  • Created and loaded a secure .env file for your OpenAI API key using python-dotenv.
  • Created a GitHub repository and prepared your project to avoid committing secrets.
Use this workflow for future projects to keep secrets safe, ensure reproducibility, and maintain clear version control for notebooks.

Watch Video

Practice Lab