Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Add new Vizro-AI tutorial for Goodreads dataset #955

Merged
merged 60 commits into from
Feb 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
5227931
First chunk
stichbury Jan 15, 2025
ddba624
Second chunk: add notebook
stichbury Jan 15, 2025
cc85686
Update settings to add pycafe to Vizro-AI docs
stichbury Jan 15, 2025
6a1402c
Update project-tutorial.md
stichbury Jan 15, 2025
edd91c4
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 15, 2025
d18ef29
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 15, 2025
7e3b650
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 15, 2025
6e5929a
Update project-tutorial.md
stichbury Jan 15, 2025
9c8849e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 15, 2025
e4fe788
Update project-tutorial.md
stichbury Jan 15, 2025
e0a4317
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 15, 2025
02bd48e
Update project-tutorial.md
stichbury Jan 15, 2025
b15c8fb
Update project-tutorial.md
stichbury Jan 15, 2025
c4912a0
Update project-tutorial.md
stichbury Jan 15, 2025
37a104c
Update vizro-ai/docs/pages/tutorials/project-tutorial.md
stichbury Jan 16, 2025
54934ee
Update vizro-ai/docs/pages/tutorials/project-tutorial.md
stichbury Jan 16, 2025
4acd120
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 16, 2025
9374244
Put prompts in copyable blocks of text
stichbury Jan 16, 2025
d831213
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 16, 2025
f225c79
Few copy edit improvements
stichbury Jan 16, 2025
3d1e7f6
Merge branch 'docs/add-vizroai-tutorial' of https://github.com/mckins…
stichbury Jan 16, 2025
1797c0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 16, 2025
21b2b5a
Linting
stichbury Jan 16, 2025
cb48c00
Merge branch 'docs/add-vizroai-tutorial' of https://github.com/mckins…
stichbury Jan 16, 2025
4e4996c
Change rules of repo for data files
stichbury Jan 16, 2025
834c8d9
Add Goodreads project and data to examples
stichbury Jan 16, 2025
c846698
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 16, 2025
2e33e0f
Pass precommit
stichbury Jan 16, 2025
fd2fe3d
Minor tweaks to data/notebook
stichbury Jan 16, 2025
174202b
Update project-tutorial.md
stichbury Jan 16, 2025
13b4b5c
Merge branch 'docs/add-vizroai-tutorial' of https://github.com/mckins…
stichbury Jan 16, 2025
5ea2d7e
Update .pre-commit-config.yaml
stichbury Jan 16, 2025
5291fdb
Update for control
stichbury Jan 16, 2025
8776cfc
Update project-tutorial.md
stichbury Jan 16, 2025
5278e33
Update the controls
stichbury Jan 16, 2025
5cfd0f9
Rename markdown file
stichbury Jan 16, 2025
1239b7a
Update mkdocs.yml
stichbury Jan 16, 2025
1bacbdd
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 16, 2025
4338889
Update .pre-commit-config.yaml
stichbury Jan 20, 2025
31b8b09
Merge branch 'docs/add-vizroai-tutorial' of https://github.com/mckins…
stichbury Jan 20, 2025
934aeb2
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 20, 2025
dee27c3
Update goodreads-tutorial.md
stichbury Jan 20, 2025
a6af91d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 20, 2025
4050a1c
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 20, 2025
29aaa6a
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 21, 2025
5520d22
Add extrahead for google site verification
stichbury Jan 23, 2025
d9e607a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2025
5a32a49
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Jan 23, 2025
ff19b31
Reverting commit on wrong branch
stichbury Jan 23, 2025
0080159
Merge branch 'docs/add-vizroai-tutorial' of https://github.com/mckins…
stichbury Jan 23, 2025
313a989
Reverting commit on wrong branch
stichbury Jan 23, 2025
d2a7c42
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Feb 3, 2025
c7ac504
Add canonical tag
stichbury Feb 4, 2025
d239ff5
Update goodreads-tutorial.md
stichbury Feb 4, 2025
b4c26d6
Create check_for_datafiles.py
stichbury Feb 4, 2025
e3e9b1c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
145ff3c
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Feb 4, 2025
8ed6027
Update canonical_tag.py
stichbury Feb 4, 2025
330d125
Merge branch 'main' into docs/add-vizroai-tutorial
stichbury Feb 4, 2025
d0c25e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update project-tutorial.md
stichbury committed Jan 15, 2025
commit 6a1402ce377cc3614bb558e8943f84cc0f12ad4d
191 changes: 191 additions & 0 deletions vizro-ai/docs/pages/tutorials/project-tutorial.md
Original file line number Diff line number Diff line change
@@ -234,4 +234,195 @@ The project isn't particularly easy to share at present either: sharing a Notebo

At this point, to share and iterate the prototype the best course of action is to transfer the generated code from the output of Vizro-AI in the Notebook into a PyCafe project.

There are three changes to the Notebook code needed for it to run on PyCafe:

1. Add `from vizro import Vizro` to the imports list
2. Add `Vizro().build(model).run()` at the end of the code block
3. Uncomment the data manager code and replace it with code needed to access the dataset:

- If you are building your own PyCafe project, you can download the dataset from the Vizro GitHub repository and upload it to the PyCafe project.
- Alternatively, you can use the code specified below to read the dataset directly from online storage.



!!! example "First dashboard"
=== "app.py"
```{.python pycafe-link}
from vizro import Vizro
import vizro.models as vm
from vizro.models.types import capture
import pandas as pd
import plotly.graph_objects as go
from vizro.models.types import capture

####### Function definitions ######
@capture("graph")
def sequence_reading(data_frame):
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=data_frame["Date Read"],
y=[1] * len(data_frame),
mode="markers",
marker=dict(size=10, color="blue"),
)
)
fig.update_layout(
title="Sequence of reading",
xaxis_title="Date Read",
yaxis_title="Sequence",
yaxis=dict(showticklabels=False, showgrid=False),
xaxis=dict(tickangle=-45),
)
return fig


@capture("graph")
def pages_books_totals(data_frame):
# Prepare data
data_frame["Date Read"] = pd.to_datetime(data_frame["Date Read"])
data_frame.sort_values("Date Read", inplace=True)
data_frame["Cumulative Pages"] = data_frame["Number of Pages"].cumsum()

# Aggregate data by year for total books read
yearly_books = data_frame.groupby(data_frame["Date Read"].dt.year).size()

# Create figure with secondary y-axis
fig = go.Figure()

# Add line for cumulative pages
fig.add_trace(
go.Scatter(
x=data_frame["Date Read"],
y=data_frame["Cumulative Pages"],
mode="lines",
name="Cumulative Pages Read",
)
)

# Add bars for books read per year
fig.add_trace(
go.Bar(
x=yearly_books.index, y=yearly_books, name="Books Read per Year", yaxis="y2"
)
)

# Set up the layout
fig.update_layout(
title="Cumulative Pages Read and Books Read per Year",
xaxis_title="Date",
yaxis=dict(title="Number of Pages"),
yaxis2=dict(title="Total Books", overlaying="y", side="right"),
)

return fig


@capture("graph")
def rating_comparison(data_frame):
# Filter out rows where 'My Rating' is 0
df = data_frame[data_frame["My Rating"] != 0]

# Create a figure
fig = go.Figure()

# Add dumbbell traces
for index, row in df.iterrows():
fig.add_trace(
go.Scatter(
x=[row["My Rating"], row["Average Rating"]],
y=[row["Title"], row["Title"]],
mode="markers+lines",
name=row["Title"],
marker=dict(size=10),
line=dict(width=2),
)
)

# Update layout
fig.update_layout(
title="Comparison of My Rating vs Average Rating",
xaxis_title="Rating",
yaxis_title="Book Title",
showlegend=False,
)

return fig

########### Read data ############
# Define the URL of the raw CSV file
# TO DO -- This will change when data is uploaded to Vizro repo
import requests
url = "https://raw.githubusercontent.com/stichbury/vizro_projects/refs/heads/main/goodreads_project/filtered_books.csv"

# Fetch the CSV file
response = requests.get(url)
response.raise_for_status() # Ensure we notice bad responses

# Load the CSV data into a pandas DataFrame
from io import StringIO
csv_data = StringIO(response.text)
df = pd.read_csv(csv_data)
df["Date Read"] = pd.to_datetime(df['Date Read'],dayfirst=True)


########### Model code ############
model = vm.Dashboard(
pages=[
vm.Page(
components=[
vm.Graph(
id="sequence_reading",
figure=sequence_reading(df),
)
],
title="Sequence of Reading",
layout=vm.Layout(grid=[[0]]),
controls=[
vm.Filter(
column="Year Published",
targets=["sequence_reading"],
selector=vm.RangeSlider(type="range_slider"),
)
],
),
vm.Page(
components=[
vm.Graph(
id="pages_books_totals",
figure=pages_books_totals(df),
)
],
title="Pages and Book Totals",
layout=vm.Layout(grid=[[0]]),
controls=[
vm.Filter(
column="Year Published",
targets=["pages_books_totals"],
selector=vm.RangeSlider(type="range_slider"),
)
],
),
vm.Page(
components=[
vm.Graph(
id="rating_comparison",
figure=rating_comparison(df),
)
],
title="Rating Comparison",
layout=vm.Layout(grid=[[0]]),
controls=[],
),
],
title="Book Reading Analysis Dashboard",
)

Vizro().build(model).run()
```