Py.Cafe

stichbury/

goodreads-reading-insights

Book Reading Insights

DocsPricing
  • app.py
  • filtered_books.csv
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 ############
df = pd.read_csv("filtered_books.csv")
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(
                    type="filter",
                    column="Date Read",
                    targets=["sequence_reading"],
                    selector=vm.DatePicker(type="date_picker", range=True),
                )
            ],
        ),
        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.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()