This guide covers the second ticket of the BandersnatchStarter project, titled "Dynamic Visualizations," as outlined in BandersnatchStarter/tickets/secondTicket.md. The objective of Sprint 2 is to create interactive visualizations of the monster data (generated in Sprint 1 using MonsterLab) by exploring visualizations in a Jupyter Notebook, implementing a chart() function in app/graph.py using Altair, and integrating the chart into the Flask web application. This guide is designed for someone with a beginner data science background, familiar with Python, pandas, and Jupyter Notebooks, but new to Flask and Git. You’ll learn to use Altair (like matplotlib or seaborn but for web charts), integrate visualizations with Flask (like sharing notebook plots online), and manage code with Git (like notebook checkpoints). The guide relates tasks to your notebook experience, guiding you toward a solution without providing complete code to encourage learning.
The second ticket focuses on:
- Notebook Exploration: Create a Jupyter Notebook to experiment with visualizations of monster data.
- Implement Chart Function: Develop a
chart()function inapp/graph.pythat creates an Altair scatter plot with specified parameters. - API Graph Integration: Serialize the chart for Flask and ensure it displays correctly with the Bandersnatch dark theme.
- Using Git: Track changes with Git for collaboration and version control.
This is like moving from plotting data in a notebook to creating interactive web visualizations, using the monster data (from MonsterLab) stored in MongoDB, and sharing your work with Git.
Before starting, ensure you’ve completed the setup and Sprint 1:
- Install Python 3, Git (git-scm.com), and Jupyter Notebook (
pip install jupyter). - Fork and clone the repository:
git clone https://github.com/your-username/BandersnatchStarter.git cd BandersnatchStarter - Set up a virtual environment:
- Windows:
python -m venv venvandvenv\Scripts\activate. - macOS/Linux:
python3 -m venv venvandsource venv/bin/activate.
- Windows:
- Install dependencies:
python -m pip install -r requirements.txt. Ensurealtair,pandas,MonsterLab, andpymongoare installed. - Complete Sprint 1, with a MongoDB database populated with at least 1,000 monsters using
MonsterLab. - Set the
SPRINTvariable inapp/main.pyto2:SPRINT = 2
- Test the Flask app:
python -m app.main(Windows) orpython3 -m app.main(macOS/Linux), and visithttp://127.0.0.1:5000. - Complete the onboarding module in your course.
You’ll need familiarity with pandas DataFrames, Python dictionaries, and Jupyter Notebooks from your data science course.
Below are the steps to complete Sprint 2, with explanations relating tasks to your Jupyter Notebook and pandas experience, incorporating the monster data from Sprint 1 and the specific requirements for the chart() function.
Goal: Use a Jupyter Notebook to experiment with Altair visualizations, like exploring plots in a notebook.
What to Do:
-
Start a Jupyter Notebook:
- In the project root, run:
jupyter notebook
- Create a new notebook (e.g.,
visualization_exploration.ipynb), like starting a new notebook for data analysis.
- In the project root, run:
-
Load Monster Data:
- Use the
MongoDBclass from Sprint 1 (app/data.py) to fetch monster data:from app.data import MongoDB import pandas as pd db = MongoDB("monsters") df = db.dataframe()
- This is like loading a CSV into a DataFrame:
df = pd.read_csv("monsters.csv"). - Inspect the DataFrame:
df.head()to see attributes likename,strength,type,speed.
- Use the
-
Experiment with Altair:
- Import Altair and create a simple scatter plot:
import altair as alt chart = alt.Chart(df).mark_circle().encode( x="strength", y="speed", color="type" ) chart.display()
- This is like
sns.scatterplot(x="strength", y="speed", hue="type", data=df)in seaborn. - Explore different chart types (e.g.,
mark_bar(),mark_line()) and encodings (e.g.,size,tooltip) in the Altair Documentation.
- Import Altair and create a simple scatter plot:
-
Test Visualizations:
- Try different combinations, e.g.,
strengthvs.speedcolored bytype, or bar charts oftypecounts. - Save the notebook to document your experiments, like saving a notebook with exploratory plots.
- Try different combinations, e.g.,
Notebook Analogy:
- The notebook is like your usual Jupyter environment for plotting with matplotlib or seaborn.
- Altair is like seaborn but creates interactive web charts, perfect for Flask integration.
- The
MongoDB.dataframe()method is likepd.read_csv()to load your dataset.
Tips:
- Use
df.columnsto check available columns (e.g.,strength,speed,type). - Save charts as HTML for testing:
chart.save("test_chart.html"), and open in a browser. - Explore the Altair Gallery for inspiration.
Goal: Create a chart() function in app/graph.py to generate a scatter plot, like a reusable plotting function in a notebook.
What to Do:
-
Open
app/graph.py:- This is where you’ll define the
chart()function, like writing a plotting function in a notebook.
- This is where you’ll define the
-
Define the Function Signature:
- Create the function with the required parameters:
from altair import Chart from pandas import DataFrame def chart(df: DataFrame, x: str, y: str, target: str) -> Chart: pass # Replace with your code
- Parameters:
df: A pandas DataFrame (fromMongoDB.dataframe()).x: A column name for the x-axis (e.g.,strength).y: A column name for the y-axis (e.g.,speed).target: A column name for coloring (e.g.,type).
- Create the function with the required parameters:
-
Create the Scatter Plot:
- Use
mark_circle()for a scatter plot, like the example:graph = Chart(df, title=f"{y} by {x} for {target}").mark_circle(size=100).encode( x=x, y=y, color=target, tooltip=Tooltip(df.columns.to_list()) )
- This is like
sns.scatterplot(x=x, y=y, hue=target, data=df)but with tooltips listing all columns.
- Use
-
Add Properties:
- Define a properties dictionary with four keys:
width,height,background, andpadding. Example:properties = { "width": 600, "height": 400, "background": "#1a1a1a", # Dark theme for Bandersnatch "padding": 20 }
- Apply with
graph.properties(**properties), like setting plot size and style in seaborn.
- Define a properties dictionary with four keys:
-
Add Configuration:
- Create a configuration dictionary to style the chart for the Bandersnatch dark theme (e.g., text color, axis styles). Example:
config = { "axis": {"labelColor": "#ffffff", "titleColor": "#ffffff"}, "title": {"color": "#ffffff"} }
- Apply with
graph.configure(**config).
- Create a configuration dictionary to style the chart for the Bandersnatch dark theme (e.g., text color, axis styles). Example:
-
Test the Function:
- Test in a notebook:
from app.graph import chart df = MongoDB("monsters").dataframe() graph = chart(df, "strength", "speed", "type") graph.display()
- Save as HTML:
graph.save("chart.html")and open in a browser.
- Test in a notebook:
Notebook Analogy:
- The
chart()function is like a notebook function that creates a seaborn plot with customizable axes and styles. - Altair’s
encode()is like specifyingx,y, andhueinsns.scatterplot(). - Properties and configuration are like
plt.figure(figsize=(...))or seaborn’sset_style().
Tips:
- Ensure
x,y, andtargetare valid DataFrame columns (checkdf.columns). - Use a dark background (e.g.,
#1a1a1a) and light text (e.g.,#ffffff) for the Bandersnatch theme. - Add a docstring explaining the function’s purpose, per PEP 8.
- Remove print statements from the final code, per PEP style.
Goal: Serialize the chart and display it in the Flask app, like sharing a notebook plot online.
What to Do:
-
Serialize the Chart:
- In
app/main.py, use thechart()function to generate a chart and serialize it:from app.graph import chart from app.data import MongoDB from flask import render_template @app.route("/view") def view(): df = MongoDB("monsters").dataframe() graph = chart(df, "strength", "speed", "type") return render_template("view.html", chart=graph.to_json())
graph.to_json()converts the chart to a JSON string for web display, likedf.to_json()in a notebook.
- In
-
Create the Jinja2 Template:
- Create
templates/view.htmlto render the chart:<!DOCTYPE html> <html> <head> <title>Monster Visualizations</title> <script src="https://cdn.jsdelivr.net/npm/vega@5"></script> <script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script> <script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script> <style> body { background-color: #1a1a1a; color: #ffffff; } </style> </head> <body> <h1>Monster Visualizations</h1> <div id="chart"></div> <script> vegaEmbed("#chart", {{ chart | safe }}); </script> </body> </html>
- This is like displaying a notebook plot in HTML, but interactive via Vega-Lite.
- Create
-
Test the Integration:
- Run the Flask app:
python -m app.main(Windows) orpython3 -m app.main(macOS/Linux). - Visit
http://127.0.0.1:5000/viewand compare to the deployed site (https://bandersnatch.herokuapp.com/view). - Check that the chart looks good with the dark Bandersnatch theme (e.g., dark background, light text).
- Run the Flask app:
Notebook Analogy:
- The
/viewroute is like a notebook cell that outputs a plot to HTML. graph.to_json()is likedf.to_html()orplt.savefig()for web sharing.- The Jinja2 template is like a markdown cell formatting a plot for display.
Tips:
- Ensure Vega-Lite scripts are included (check URLs in the Altair documentation).
- Use the
safefilter in{{ chart | safe }}to render JSON correctly. - Check browser developer tools (F12) for JavaScript errors if the chart doesn’t display.
Goal: Use Git to manage code, like saving notebook checkpoints.
What to Do:
-
Create a Branch:
- Create a branch for Sprint 2:
git checkout -b sprint-2
- Create a branch for Sprint 2:
-
Commit Changes:
- After editing
app/graph.py,app/main.py,templates/view.html, and your notebook:git add . git commit -m "Implement chart function and Flask integration for Sprint 2"
- After editing
-
Push to GitHub:
- Push your branch:
git push origin sprint-2
- Create a pull request on your GitHub fork, like submitting a notebook for review.
- Push your branch:
Notebook Analogy:
- A Git branch is like a separate notebook for a task.
- Committing is like saving a notebook, and pushing is like uploading it.
Tips:
- Run
git statusto check changes, like inspecting modified notebook cells. - Use clear commit messages (e.g., “Add Altair chart function”).
- Check GitHub’s documentation for help.
Goal: Submit the required deliverables for the ticket.
What to Do:
-
Forked Repository Link:
- Ensure changes are pushed to your GitHub fork.
- Copy the repository URL (e.g.,
https://github.com/your-username/BandersnatchStarter).
-
Loom Video:
- Record a Loom video answering the prompt in the
Submit Your Deliverablesassignment. - Demonstrate:
- The Jupyter Notebook with visualization experiments.
- The
/viewroute showing the chart locally. - Explaining the
chart()function inapp/graph.pyand its styling. - Confirming the chart matches the Bandersnatch dark theme.
- Record a Loom video answering the prompt in the
-
Submit in Course:
- Submit the repository link and Loom video link in your course platform.
Notebook Analogy:
- The Loom video is like presenting your notebook plots in a class.
- Submitting links is like sharing a notebook file and a recorded demo.
Tips:
- Test the
/viewroute locally to match the deployed site. - Keep the video concise, focusing on notebook exploration, the
chart()function, and web integration.
- Altair vs. Seaborn/Matplotlib: Altair is like seaborn but creates interactive web charts. Use
mark_circle()likesns.scatterplot(). - MongoDB vs. pandas: Use
MongoDB.dataframe()likepd.read_csv()to load data. - Flask vs. Notebooks: The
/viewroute is like a notebook cell outputting a plot to HTML. Jinja2 templates are like markdown cells for web display. - Git vs. Checkpoints: Git tracks changes across files, like notebook checkpoints. Branch per sprint, commit often.
- Code Style:
- Add docstrings for the
chart()function, like comments in a notebook. - Follow PEP 8 (e.g., no inline
print()statements in final code). - Remove extraneous comments, like cleaning notebook cells.
- Add docstrings for the
- Debugging:
- Use
print(df.head())in the notebook to inspect data, but remove fromapp/graph.py. - Use try-except in
chart()for invalid columns:try: # Chart code except Exception as e: print(f"Error: {e}")
- Check browser developer tools (F12) for Vega-Lite errors.
- Use
- Resources:
- Altair Documentation: For chart syntax and examples.
- pandas Documentation: For DataFrame operations.
- Flask Documentation: For web routes.
- Git for Beginners: For Git basics.
- Invalid DataFrame Columns: Ensure
x,y, andtargetare indf.columns. Printdf.columnsin the notebook to verify. - Chart Not Rendering: Check Vega-Lite script URLs in
view.htmland browser console (F12) for errors. - Theme Mismatch: Adjust
background,labelColor, andtitleColorin properties/config to match the dark theme (e.g.,#1a1a1abackground,#fffffftext). - Git Errors: If
git pushfails, ensure you’re onsprint-2(git branch) and pushing to your fork. - Notebook Data Issues: If
dfis empty, verify theMongoDBclass and database connection from Sprint 1.
- Test Your Work:
- Experiment with multiple charts in the notebook and save results.
- Test the
chart()function with differentx,y, andtargetvalues. - Verify the
/viewroute matches the deployed site’s appearance.
- Prepare for Sprint 3: Ensure monster data has varied attributes for machine learning.
- Git Workflow: Commit and push changes, create a pull request for
sprint-2. - Deliverables: Submit your repository link and Loom video in your course.
- Create a Jupyter Notebook and load monster data with
MongoDB.dataframe(). - Experiment with Altair scatter plots, bar charts, and tooltips.
- Implement the
chart()function inapp/graph.pywith properties and configuration for the Bandersnatch theme. - Update
app/main.pyandtemplates/view.htmlto display the chart. - Test at
http://127.0.0.1:5000/view. - Commit changes to a
sprint-2branch and push to GitHub. - Record a Loom video and submit deliverables.
Sprint 2 builds on your pandas and notebook skills to create interactive visualizations with Altair, using monster data from Sprint 1. By exploring in a notebook, implementing a chart() function, and integrating with Flask, you’ll create a web-based visualization, like sharing a notebook plot online. Treat Altair like seaborn, Flask routes like notebook outputs, and Git like checkpoints. Work through each step, test frequently, and leverage your data science experience to succeed.