afshin-dini commited on
Commit
fc611f1
·
1 Parent(s): c096c50

Add bar plot

Browse files
.gitignore CHANGED
@@ -10,6 +10,9 @@ poetry.lock
10
  .cache
11
  .pytest_cache/
12
  src/ai_dashboard/__pycache__/
 
 
 
13
 
14
  # mypy files
15
  .mypy_cache/
 
10
  .cache
11
  .pytest_cache/
12
  src/ai_dashboard/__pycache__/
13
+ src/ai_dashboard/plot_plugin/__pycache__/
14
+ src/ai_dashboard/table_plugin/__pycache__/
15
+
16
 
17
  # mypy files
18
  .mypy_cache/
src/ai_dashboard/plot_plugin/bar_plot.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """This is the bar plot module for AI Dashboard."""
2
+
3
+ from typing import Any
4
+ import logging
5
+
6
+ import plotly.express as px
7
+ from dash import html, dcc
8
+ from dash.dependencies import Input, Output
9
+
10
+ from ..base import BasePlotPlugin
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ class BarPlotPlugin(BasePlotPlugin):
16
+ """Bar Plot Plugin for AI Dashboard."""
17
+
18
+ name = "Bar Chart"
19
+
20
+ def dropdown(self, id_suffix: str, label: str) -> Any:
21
+ """Generate a dropdown for plot controls."""
22
+ cols = self.dataframe.columns.tolist()
23
+ return html.Div(
24
+ [
25
+ html.Label(label),
26
+ dcc.Dropdown(
27
+ id={"type": "control", "plot": self.name, "axis": id_suffix},
28
+ options=[{"label": c, "value": c} for c in cols], # type: ignore
29
+ value=cols[0],
30
+ clearable=False,
31
+ persistence=True,
32
+ persistence_type="memory",
33
+ ),
34
+ ],
35
+ style={"width": "170px", "marginRight": "12px"},
36
+ )
37
+
38
+ def controls(self) -> Any:
39
+ """Generate plot controls."""
40
+ return html.Div(
41
+ [
42
+ self.dropdown("x", "X-Axis"),
43
+ self.dropdown("y", "Y-Axis"),
44
+ ],
45
+ style={"display": "flex", "flexWrap": "wrap", "gap": "12px"},
46
+ )
47
+
48
+ def render(self, **kwargs: Any) -> Any: # pylint: disable=W0201
49
+ """Render the bar plot."""
50
+ x_axis = kwargs.get("x_axis")
51
+ y_axis = kwargs.get("y_axis")
52
+ fig = px.bar(self.dataframe, x=x_axis, y=y_axis, text_auto=True)
53
+ return dcc.Graph(figure=fig)
54
+
55
+ def register_callbacks(self, app: Any) -> None:
56
+ """Register callbacks for interactivity."""
57
+
58
+ @app.callback( # type: ignore
59
+ Output({"type": "plot-output", "plot": self.name}, "children"),
60
+ Input({"type": "control", "plot": self.name, "axis": "x"}, "value"),
61
+ Input({"type": "control", "plot": self.name, "axis": "y"}, "value"),
62
+ )
63
+ def update_bar(x_axis: str, y_axis: str) -> Any:
64
+ """Update the bar chart based on dropdown selections."""
65
+ return self.render(x_axis=x_axis, y_axis=y_axis)