Py.Cafe

MarcSkovMadsen/

panel-iris-analysis

Iris Species Analysis

DocsPricing
  • app.py
  • 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
# Inspired by https://py.cafe/vizro-official/vizro-iris-analysis
import panel as pn
import param
import plotly.express as px


@pn.cache()
def get_data(species_filter="ALL"):
    if species_filter == "ALL":
        return px.data.iris()
    df = get_data("ALL")
    return df[df["species"] == species_filter]


SPECIES = get_data()["species"].unique().tolist()
PLOTLY_TEMPLATE = "plotly_dark" if pn.config.theme == "dark" else "plotly_white"
PLOTLY_COLORS = px.colors.qualitative.G10
ACCENT = PLOTLY_COLORS[1]

pn.extension(
    "plotly",
    sizing_mode="stretch_both",
    loading_spinner="dots",
    loading_indicator=True,
    loading_color=ACCENT,
)


class IrisDashboard(param.Parameterized):
    species_filter = param.ObjectSelector(default="ALL", objects=["ALL"] + SPECIES)

    @pn.depends("species_filter")
    def scatter_plot(self, template=PLOTLY_TEMPLATE, colors=PLOTLY_COLORS):
        filtered_df = get_data(self.species_filter)
        return px.scatter(
            filtered_df,
            x="sepal_length",
            y="petal_width",
            color="species",
            template=template,
            color_discrete_sequence=colors,
        )
        return fig

    @pn.depends("species_filter")
    def histogram(self, template=PLOTLY_TEMPLATE, colors=PLOTLY_COLORS):
        filtered_df = get_data(self.species_filter)
        return px.histogram(
            filtered_df,
            x="sepal_width",
            color="species",
            template=template,
            color_discrete_sequence=colors,
        )

    def plots(self):
        return pn.Column(self.scatter_plot, self.histogram)


dashboard = IrisDashboard()

pn.template.FastListTemplate(
    title="Iris Dashboard",
    sidebar=[dashboard.param.species_filter],
    main=[dashboard.plots],
    main_layout=None,
    accent=ACCENT,
).servable()