Jupyter Notebook is one of the better tools for Python developers, data scientists, and anyone who likes their code interactive. Debugging a complex function, experimenting with visualizations, or just sketching out a one-off script all benefit from the cell-based workflow. This post sets up Jupyter inside a Python virtual environment (clean isolation, no system Python contamination) and then puts it to work with a small RPG-inspired demo.
Why use Jupyter Notebook?#
Jupyter Notebook offers a range of benefits that make it indispensable for Python workflows:
- Interactive coding: Divide your code into manageable cells, run them incrementally, and experiment without restarting everything.
- Visualization friendly: Render charts and graphs directly inline, perfect for spotting trends or showcasing insights.
- Markdown support: Mix narrative explanations with code for clarity and effective communication.
- Portable notebooks: Share
.ipynbfiles or export them in static formats like HTML or PDF for wide compatibility.
Installing Jupyter Notebook in a virtual environment#
Step 1: Activate your virtual environment#
Ensure you have a Python virtual environment set up to keep dependencies isolated. If you’re unsure how, refer to this guide for help. Once your environment is ready, activate it:
pyenv activate my-project-envFor environments created with venv, use:
source my-project-env/bin/activateStep 2: Install Jupyter Notebook#
With your virtual environment active, install Jupyter Notebook using pip:
pip install notebookVerify the installation was successful by checking the version:
jupyter --versionStep 3: Launch Jupyter Notebook#
Start the Jupyter Notebook server:
jupyter notebookThis command opens a web-based interface in your default browser. If it doesn’t launch automatically, look for the URL in the terminal output (e.g., http://localhost:8888/).
Using Jupyter Notebook#
Creating a new Notebook#
- Click New in the Jupyter interface, select Notebook, and select Python 3 (ipykernel) to create a new notebook.
- Each notebook consists of cells, where you can write and execute Python code or markdown for documentation.
Example 1: rolling for initiative#
Here’s a quick Python demo inspired by RPGs. Let’s simulate initiative rolls for a party of adventurers:
import random
characters = ["Rogue", "Fighter", "Wizard", "Cleric"]
initiative = {char: random.randint(1, 20) for char in characters}
sorted_initiative = sorted(initiative.items(), key=lambda x: x[1], reverse=True)
for position, (char, roll) in enumerate(sorted_initiative, start=1):
print(f"{position}: {char} rolled a {roll}")Run the cell to see which character gets to act first in the battle!
Visualization example: damage over time#
Jupyter makes data visualization simple and interactive. Let’s plot a damage-over-time graph for a Fireball spell:
import matplotlib.pyplot as plt
# If matplotlib is not installed, install it directly in the notebook using the command below.
# The '!' lets Jupyter execute shell commands within a code cell.
# Uncomment the line below:
# !pip install matplotlib
turns = list(range(1, 6)) # Damage dealt over 5 turns
damage = [8 * t for t in turns] # Fireball deals 8 damage per turn
plt.plot(turns, damage, marker="o", label="Fireball Damage")
plt.xlabel("Turns")
plt.ylabel("Total Damage")
plt.title("Damage Over Time")
plt.legend()
plt.show()This graph visualizes the spell’s escalating damage, helping you strategize more effectively in your campaigns.
Exporting notebooks#
Sharing your work is easy with Jupyter. Save your notebook as a .ipynb file to retain its interactivity or export it as a static format like HTML or PDF. Use the File menu and select Save and Export Notebook As to explore the available options.
Closing notes#
Jupyter Notebook combines the power of Python with an intuitive, interactive interface. It supports coding, visualization, and documentation in one place, which makes it ideal for prototyping, teaching, or sketching out an idea.
The dice-roll demo is a toy, but the instinct behind it isn’t: turning a fuzzy question into a few cells you can run and tweak is most of what the notebook is good for. It’s the same programming-brain-meets-tabletop habit that shows up in my daily-note system, initiative order and character sheets included.
Additional resources#
- Jupyter Notebook documentation
- Real Python: Jupyter Notebook tutorial
- Mastering Python installation on macOS with Homebrew and pyenv — set up the virtual environment this runs in
