Post

Rolling Initiative with Jupyter Notebook in Python Virtual Environments

Rolling Initiative with Jupyter Notebook in Python Virtual Environments

Jupyter Notebook isn’t just a tool—it’s a game-changer for Python developers, data scientists, and anyone who likes their code interactive and visual. Whether you’re debugging a complex function or experimenting with creative data visualizations, Jupyter gives you the ability to test, document, and share your work seamlessly. In this post, we’ll cover installing Jupyter Notebook in a Python virtual environment and using it to run fun, RPG-inspired Python demos.


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 .ipynb files 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:

1
pyenv activate my-project-env

For environments created with venv, use:

1
source my-project-env/bin/activate

Step 2: Install Jupyter Notebook

With your virtual environment active, install Jupyter Notebook using pip:

1
pip install notebook

Verify the installation was successful by checking the version:

1
jupyter --version

Step 3: Launch Jupyter Notebook

Start the Jupyter Notebook server:

1
jupyter notebook

This 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:

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.


Wrapping Up

Jupyter Notebook combines the power of Python with an intuitive, interactive interface. It’s a versatile tool that supports coding, visualization, and documentation all in one place—ideal for prototyping, teaching, or developing new ideas.

Have questions or ideas for using Jupyter in creative ways? Drop a comment or reach out on Discord. Let’s keep building and learning together!


Additional Resources

This post is licensed under CC BY 4.0 by the author.
Keyboard Shortcuts