Compare commits
21
Commits
650df5fa55
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
249d8f1703 | ||
|
|
81c44f4a0b | ||
|
|
db9ea8197f | ||
|
|
43f5277b75 | ||
|
|
74af4c4018 | ||
|
|
1ceea5c614 | ||
|
|
a4c16662db | ||
|
|
96a26a8ea0 | ||
|
|
2dab2139ce | ||
|
|
e55e2b9b97 | ||
|
|
44be992d0a | ||
|
|
dd6de9c83b | ||
|
|
9bf8283d34 | ||
|
|
6367cc0b76 | ||
|
|
95e6e86cd5 | ||
|
|
c01e729bd5 | ||
|
|
f64cf5ed4d | ||
|
|
aad677e9be | ||
|
|
2faaa900a3 | ||
|
|
9773de31cb | ||
|
|
aa04d5b282 |
No files matched your search
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"[python]": {
|
||||||
|
"editor.defaultFormatter": "ms-python.black-formatter"
|
||||||
|
},
|
||||||
|
"python.formatting.provider": "none"
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
# Setup
|
||||||
|
|
||||||
|
_Works on my machine..._
|
||||||
|
|
||||||
|
**uname -a**
|
||||||
|
|
||||||
|
> Linux marc-hp 5.15.0-70-generic #77-Ubuntu SMP Tue Mar 21 14:02:37 UTC 2023 x86*64 x86_64 x86_64 GNU/Linux*
|
||||||
|
|
||||||
|
**python3 --version**
|
||||||
|
|
||||||
|
> Python 3.10.6
|
||||||
|
|
||||||
|
**pip --version**
|
||||||
|
|
||||||
|
> pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
|
||||||
|
|
||||||
|
## Environment
|
||||||
|
|
||||||
|
### Activate
|
||||||
|
|
||||||
|
1. ```bash
|
||||||
|
cd dashboardX
|
||||||
|
```
|
||||||
|
1. ```bash
|
||||||
|
python3 -m venv env
|
||||||
|
```
|
||||||
|
1. ```bash
|
||||||
|
source env/bin/activate
|
||||||
|
```
|
||||||
|
1. ```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deactivate
|
||||||
|
|
||||||
|
```bash
|
||||||
|
deactivate
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 main.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## Packages
|
||||||
|
|
||||||
|
### Add new Package
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install <package-name>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Update requirements.txt
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip freeze > requirements.txt
|
||||||
|
```
|
||||||
+50
-15
@@ -12,28 +12,63 @@ app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|||||||
|
|
||||||
df = pd.DataFrame({'y': np.random.normal(loc=0, scale=10, size=1000),
|
df = pd.DataFrame({'y': np.random.normal(loc=0, scale=10, size=1000),
|
||||||
'x': np.random.normal(loc=10, scale=2, size=1000)})
|
'x': np.random.normal(loc=10, scale=2, size=1000)})
|
||||||
|
df_min = math.floor(df["y"].min())
|
||||||
|
df_max = math.ceil(df["y"].max())
|
||||||
|
|
||||||
app.layout = html.Div([html.H1("Dashboard 2"),
|
app.layout = html.Div(
|
||||||
dbc.Row([dbc.Col([dcc.Dropdown(options=['red', 'green', 'blue'], value='red', id='color', multi=False)], width=6),
|
[
|
||||||
dbc.Col([dcc.Slider(min=math.floor(df['y'].min()), max=math.ceil(df['y'].max()), id="min_value")
|
html.H1("Dashboard 2"),
|
||||||
], width=6)
|
dbc.Row(
|
||||||
]),
|
[
|
||||||
dbc.Row([dbc.Col([dcc.Graph(id="graph_1")], width=6),
|
dbc.Col(
|
||||||
dbc.Col([dcc.Graph(id="graph_2")], width=6)
|
[
|
||||||
])], className="m-4")
|
dcc.Dropdown(
|
||||||
|
options=["red", "green", "blue"],
|
||||||
|
value="red",
|
||||||
|
id="color",
|
||||||
|
multi=False,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.RangeSlider(
|
||||||
|
min=df_min,
|
||||||
|
max=df_max,
|
||||||
|
value=[df_min, df_max],
|
||||||
|
id="range",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col([dcc.Graph(id="graph_1")], width=6),
|
||||||
|
dbc.Col([dcc.Graph(id="graph_2")], width=6),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
],
|
||||||
|
className="m-4",
|
||||||
|
)
|
||||||
|
|
||||||
@app.callback(Output("graph_1", "figure"), Input("color", "value"))
|
# Now the color selecion and the slider works for both diagrams
|
||||||
|
@app.callback(Output("graph_1", "figure"), Input("color", "value"), Input("range", "value"))
|
||||||
|
|
||||||
def update_graph_1(dropdown_value_color):
|
def update_graph_1(color, range):
|
||||||
fig = px.histogram(df, x="y", color_discrete_sequence=[dropdown_value_color])
|
# this filter is used twice. This could be improved
|
||||||
|
dff = df[(df['y'] > range[0]) & (df['y'] < range[1])]
|
||||||
|
fig = px.histogram(dff, x="y", color_discrete_sequence=[color])
|
||||||
fig.update_layout()
|
fig.update_layout()
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
@app.callback(Output("graph_2", "figure"), Input("min_value", "value"))
|
@app.callback(Output("graph_2", "figure"), Input("color", "value"), Input("range", "value"))
|
||||||
|
|
||||||
def update_graph_2(min_value):
|
def update_graph_2(color, range):
|
||||||
dff = df[df['y']> min_value]
|
dff = df[(df['y'] > range[0]) & (df['y'] < range[1])]
|
||||||
fig = px.scatter(dff, x='x', y='y')
|
fig = px.scatter(dff, x='x', y='y', color_discrete_sequence=[color])
|
||||||
fig.update_layout()
|
fig.update_layout()
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
blinker==1.6.2
|
||||||
|
click==8.1.3
|
||||||
|
dash==2.9.3
|
||||||
|
dash-bootstrap-components==1.4.1
|
||||||
|
dash-core-components==2.0.0
|
||||||
|
dash-html-components==2.0.0
|
||||||
|
dash-table==5.0.0
|
||||||
|
Flask==2.3.2
|
||||||
|
itsdangerous==2.1.2
|
||||||
|
Jinja2==3.1.2
|
||||||
|
MarkupSafe==2.1.2
|
||||||
|
numpy==1.24.3
|
||||||
|
packaging==23.1
|
||||||
|
pandas==2.0.1
|
||||||
|
plotly==5.14.1
|
||||||
|
python-dateutil==2.8.2
|
||||||
|
pytz==2023.3
|
||||||
|
six==1.16.0
|
||||||
|
tenacity==8.2.2
|
||||||
|
tzdata==2023.3
|
||||||
|
Werkzeug==2.3.4
|
||||||
File renamed without changes.
+162
-16
@@ -7,35 +7,181 @@ import dash_bootstrap_components as dbc
|
|||||||
|
|
||||||
# new: plotly template="plotly_white", https://plotly.com/python/templates/
|
# new: plotly template="plotly_white", https://plotly.com/python/templates/
|
||||||
|
|
||||||
|
|
||||||
|
# Things done in here:
|
||||||
|
# 1: Copied first tab content into second
|
||||||
|
# 2: Changed IDs of everthing
|
||||||
|
# 3: changed plottypes for tab 2
|
||||||
|
# 4: Added the 3rd tab
|
||||||
|
|
||||||
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
||||||
|
|
||||||
df = pd.DataFrame({'y': np.random.normal(loc=0, scale=10, size=1000), 'x': np.random.normal(loc=10, scale=2, size=1000)})
|
df = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"y": np.random.normal(loc=0, scale=10, size=1000),
|
||||||
|
"x": np.random.normal(loc=10, scale=2, size=1000),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
app.layout = html.Div([html.Div([html.H1("Dashboard 3")], className="header"), html.Div([dcc.Tabs(id="tabs", children=[
|
app.layout = html.Div(
|
||||||
dcc.Tab(label='Tab One', id="tab_1_graphs", children=[html.Div([
|
[
|
||||||
dbc.Row([dbc.Col([dcc.Dropdown(options=['red','green','blue'], value='red', id='color', multi=False)], width=6),
|
html.Div([html.H1("Dashboard 3")], className="header"),
|
||||||
dbc.Col([dcc.Slider(min=math.floor(df['y'].min()),max=math.ceil(df['y'].max()),id="min_value")], width=6)]),
|
html.Div(
|
||||||
dbc.Row([dbc.Col([dcc.Graph(id="graph_1")],width=6),
|
[
|
||||||
dbc.Col([dcc.Graph(id="graph_2")],width=6)])], className="tab_content"),]),
|
dcc.Tabs(
|
||||||
dcc.Tab(label='Tab Two', id="tab_2_graphs",children=[html.Div([],className="tab_content")]),])], className="content")])
|
id="tabs",
|
||||||
|
children=[
|
||||||
|
dcc.Tab(
|
||||||
|
label="Tab One",
|
||||||
|
id="tab_1_graphs",
|
||||||
|
children=[
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Dropdown(
|
||||||
|
options=[
|
||||||
|
"red",
|
||||||
|
"green",
|
||||||
|
"blue",
|
||||||
|
],
|
||||||
|
value="red",
|
||||||
|
id="color_1",
|
||||||
|
multi=False,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Slider(
|
||||||
|
min=math.floor(
|
||||||
|
df["y"].min()
|
||||||
|
),
|
||||||
|
max=math.ceil(
|
||||||
|
df["y"].max()
|
||||||
|
),
|
||||||
|
id="min_value_1",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_1_1")], width=6
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_1_2")], width=6
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
],
|
||||||
|
className="tab_content",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
dcc.Tab(
|
||||||
|
label="Tab Two",
|
||||||
|
id="tab_2_graphs",
|
||||||
|
children=[html.Div([
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Dropdown(
|
||||||
|
options=[
|
||||||
|
"red",
|
||||||
|
"green",
|
||||||
|
"blue",
|
||||||
|
],
|
||||||
|
value="red",
|
||||||
|
id="color_2",
|
||||||
|
multi=False,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Slider(
|
||||||
|
min=math.floor(
|
||||||
|
df["y"].min()
|
||||||
|
),
|
||||||
|
max=math.ceil(
|
||||||
|
df["y"].max()
|
||||||
|
),
|
||||||
|
id="min_value_2",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_2_1")], width=6
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_2_2")], width=6
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
], className="tab_content")],
|
||||||
|
),dcc.Tab(
|
||||||
|
label="Tab Three",
|
||||||
|
id="tab_3_graphs",
|
||||||
|
children=[html.Div([html.Iframe(src="https://dashboard.m-g.tech", height="500px;", width="100%" )], className="tab_content")],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
className="content",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
@app.callback(Output("graph_1", "figure"), Input("color", "value"))
|
|
||||||
|
|
||||||
def update_graph_1(dropdown_value_color):
|
@app.callback(Output("graph_1_1", "figure"), Input("color_1", "value"))
|
||||||
|
def update_graph_1_1(dropdown_value_color):
|
||||||
fig = px.histogram(df, x="y", color_discrete_sequence=[dropdown_value_color])
|
fig = px.histogram(df, x="y", color_discrete_sequence=[dropdown_value_color])
|
||||||
fig.update_layout(template="plotly_white")
|
fig.update_layout(template="plotly_white")
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
@app.callback(Output("graph_2", "figure"),Input("min_value", "value"))
|
|
||||||
|
|
||||||
def update_graph_2(min_value):
|
@app.callback(Output("graph_1_2", "figure"), Input("min_value_1", "value"))
|
||||||
|
def update_graph_1_2(min_value):
|
||||||
if min_value:
|
if min_value:
|
||||||
dff = df[df['y'] > min_value]
|
dff = df[df["y"] > min_value]
|
||||||
else:
|
else:
|
||||||
dff = df
|
dff = df
|
||||||
fig = px.scatter(dff, x='x', y='y')
|
fig = px.scatter(dff, x="x", y="y")
|
||||||
fig.update_layout(template="plotly_white")
|
fig.update_layout(template="plotly_white")
|
||||||
return fig
|
return fig
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run_server(debug=True, port=8000)
|
@app.callback(Output("graph_2_1", "figure"), Input("color_2", "value"))
|
||||||
|
def update_graph_2_1(dropdown_value_color):
|
||||||
|
fig = px.box(df, x="y", color_discrete_sequence=[dropdown_value_color])
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(Output("graph_2_2", "figure"), Input("min_value_2", "value"))
|
||||||
|
def update_graph_2_2(min_value):
|
||||||
|
if min_value:
|
||||||
|
dff = df[df["y"] > min_value]
|
||||||
|
else:
|
||||||
|
dff = df
|
||||||
|
fig = px.density_contour(dff, x="x", y="y")
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run_server(debug=True, port=8000)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
blinker==1.6.2
|
||||||
|
click==8.1.3
|
||||||
|
dash==2.9.3
|
||||||
|
dash-bootstrap-components==1.4.1
|
||||||
|
dash-core-components==2.0.0
|
||||||
|
dash-html-components==2.0.0
|
||||||
|
dash-table==5.0.0
|
||||||
|
Flask==2.3.2
|
||||||
|
itsdangerous==2.1.2
|
||||||
|
Jinja2==3.1.2
|
||||||
|
MarkupSafe==2.1.2
|
||||||
|
numpy==1.24.3
|
||||||
|
packaging==23.1
|
||||||
|
pandas==2.0.1
|
||||||
|
plotly==5.14.1
|
||||||
|
python-dateutil==2.8.2
|
||||||
|
pytz==2023.3
|
||||||
|
six==1.16.0
|
||||||
|
tenacity==8.2.2
|
||||||
|
tzdata==2023.3
|
||||||
|
Werkzeug==2.3.4
|
||||||
@@ -0,0 +1,255 @@
|
|||||||
|
import math
|
||||||
|
from dash import Dash, dcc, html, Input, Output
|
||||||
|
import plotly.express as px
|
||||||
|
import plotly.graph_objects as go
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import dash_bootstrap_components as dbc
|
||||||
|
from sklearn.datasets import make_blobs
|
||||||
|
|
||||||
|
# new: more than one plot in a callback
|
||||||
|
# new: one plot as an input for another plot
|
||||||
|
# new: plotly go object
|
||||||
|
|
||||||
|
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
||||||
|
|
||||||
|
df = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"y": np.random.normal(loc=0, scale=10, size=1000),
|
||||||
|
"x": np.random.normal(loc=10, scale=2, size=1000),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# define cluster colors
|
||||||
|
# https://loading.io/color/feature/Spectral-10/
|
||||||
|
COLORS = {
|
||||||
|
"0": "#9e0142",
|
||||||
|
"1": "#d53e4f",
|
||||||
|
"2": "#f46d43",
|
||||||
|
"3": "#fdae61",
|
||||||
|
"4": "#fee08b",
|
||||||
|
"5": "#e6f598",
|
||||||
|
"6": "#abdda4",
|
||||||
|
"7": "#66c2a5",
|
||||||
|
"8": "#3288bd",
|
||||||
|
"9": "#5e4fa2",
|
||||||
|
}
|
||||||
|
|
||||||
|
X, y = make_blobs(n_samples=300, centers=3, n_features=2, random_state=0)
|
||||||
|
|
||||||
|
cluster_df = pd.DataFrame(data=X, columns=["X", "Y"])
|
||||||
|
cluster_df["cluster"] = [str(i) for i in y]
|
||||||
|
|
||||||
|
app.layout = html.Div(
|
||||||
|
[
|
||||||
|
html.Div([html.H1("Dashboard 4")], className="header"),
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dcc.Tabs(
|
||||||
|
id="tabs",
|
||||||
|
children=[
|
||||||
|
dcc.Tab(
|
||||||
|
label="Tab One",
|
||||||
|
id="tab_1_graphs",
|
||||||
|
children=[
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Dropdown(
|
||||||
|
options=[
|
||||||
|
"red",
|
||||||
|
"green",
|
||||||
|
"blue",
|
||||||
|
],
|
||||||
|
value="red",
|
||||||
|
id="color",
|
||||||
|
multi=False,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Slider(
|
||||||
|
min=math.floor(
|
||||||
|
df["y"].min()
|
||||||
|
),
|
||||||
|
max=math.ceil(
|
||||||
|
df["y"].max()
|
||||||
|
),
|
||||||
|
id="min_value",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_1")], width=6
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_2")], width=6
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(width=6),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_69")], width=6
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
],
|
||||||
|
className="tab_content",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
dcc.Tab(
|
||||||
|
label="Tab Two",
|
||||||
|
id="tab_2_graphs",
|
||||||
|
children=[
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Slider(
|
||||||
|
1,
|
||||||
|
10,
|
||||||
|
1,
|
||||||
|
value=3,
|
||||||
|
id="slider_1",
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_3")], width=8
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_4")], width=4
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
],
|
||||||
|
className="tab_content",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
className="content",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(Output("graph_1", "figure"), Input("color", "value"))
|
||||||
|
def update_graph_1(dropdown_value_color):
|
||||||
|
fig = px.histogram(df, x="y", color_discrete_sequence=[dropdown_value_color])
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(Output("graph_2", "figure"), Input("min_value", "value"))
|
||||||
|
def update_graph_2(min_value):
|
||||||
|
if min_value:
|
||||||
|
dff = df[df["y"] > min_value]
|
||||||
|
else:
|
||||||
|
dff = df
|
||||||
|
fig = px.scatter(dff, x="x", y="y")
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
# 7a
|
||||||
|
@app.callback(Output("graph_69", "figure"), Input("graph_2", "relayoutData"))
|
||||||
|
def update_graph_2(selected_data):
|
||||||
|
if selected_data is None or (
|
||||||
|
isinstance(selected_data, dict) and "xaxis.range[0]" not in selected_data
|
||||||
|
):
|
||||||
|
dff = df
|
||||||
|
else:
|
||||||
|
dff = df[
|
||||||
|
(df["x"] >= selected_data.get("xaxis.range[0]"))
|
||||||
|
& (df["x"] <= selected_data.get("xaxis.range[1]"))
|
||||||
|
& (df["y"] >= selected_data.get("yaxis.range[0]"))
|
||||||
|
& (df["y"] <= selected_data.get("yaxis.range[1]"))
|
||||||
|
]
|
||||||
|
fig = px.scatter(dff, x="x", y="y")
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(
|
||||||
|
Output("graph_3", "figure"),
|
||||||
|
Output("graph_4", "figure"),
|
||||||
|
Input("graph_3", "relayoutData"),
|
||||||
|
Input("slider_1", "value"),
|
||||||
|
)
|
||||||
|
def update_graph_3_and_4(selected_data, sliderValue):
|
||||||
|
## 7b
|
||||||
|
# forcing update of x, y and cluster_df with the proper am
|
||||||
|
X, y = make_blobs(
|
||||||
|
n_samples=100 * sliderValue, centers=sliderValue, n_features=2, random_state=0
|
||||||
|
)
|
||||||
|
cluster_df = pd.DataFrame(data=X, columns=["X", "Y"])
|
||||||
|
cluster_df["cluster"] = [str(i) for i in y]
|
||||||
|
|
||||||
|
if selected_data is None or (
|
||||||
|
isinstance(selected_data, dict) and "xaxis.range[0]" not in selected_data
|
||||||
|
):
|
||||||
|
cluster_dff = cluster_df
|
||||||
|
else:
|
||||||
|
cluster_dff = cluster_df[
|
||||||
|
(cluster_df["X"] >= selected_data.get("xaxis.range[0]"))
|
||||||
|
& (cluster_df["X"] <= selected_data.get("xaxis.range[1]"))
|
||||||
|
& (cluster_df["Y"] >= selected_data.get("yaxis.range[0]"))
|
||||||
|
& (cluster_df["Y"] <= selected_data.get("yaxis.range[1]"))
|
||||||
|
]
|
||||||
|
|
||||||
|
fig3 = px.scatter(
|
||||||
|
cluster_dff,
|
||||||
|
x="X",
|
||||||
|
y="Y",
|
||||||
|
color="cluster",
|
||||||
|
color_discrete_map=COLORS,
|
||||||
|
category_orders={"cluster": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]},
|
||||||
|
height=750,
|
||||||
|
)
|
||||||
|
fig3.update_layout(template="plotly_white", coloraxis_showscale=False)
|
||||||
|
fig3.update_traces(marker=dict(size=8))
|
||||||
|
|
||||||
|
group_counts = cluster_dff[["cluster", "X"]].groupby("cluster").count()
|
||||||
|
|
||||||
|
fig4 = go.Figure(
|
||||||
|
data=[
|
||||||
|
go.Bar(
|
||||||
|
x=group_counts.index,
|
||||||
|
y=group_counts["X"],
|
||||||
|
marker_color=[COLORS.get(i) for i in group_counts.index],
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
fig4.update_layout(
|
||||||
|
height=750,
|
||||||
|
template="plotly_white",
|
||||||
|
title="<b>Counts per cluster</b>",
|
||||||
|
xaxis_title="cluster",
|
||||||
|
title_font_size=25,
|
||||||
|
)
|
||||||
|
return fig3, fig4
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run_server(debug=True, port=8012)
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
blinker==1.6.2
|
||||||
|
click==8.1.3
|
||||||
|
dash==2.9.3
|
||||||
|
dash-bootstrap-components==1.4.1
|
||||||
|
dash-core-components==2.0.0
|
||||||
|
dash-html-components==2.0.0
|
||||||
|
dash-table==5.0.0
|
||||||
|
Flask==2.3.2
|
||||||
|
itsdangerous==2.1.2
|
||||||
|
Jinja2==3.1.2
|
||||||
|
joblib==1.2.0
|
||||||
|
MarkupSafe==2.1.2
|
||||||
|
numpy==1.24.3
|
||||||
|
packaging==23.1
|
||||||
|
pandas==2.0.1
|
||||||
|
plotly==5.14.1
|
||||||
|
python-dateutil==2.8.2
|
||||||
|
pytz==2023.3
|
||||||
|
scikit-learn==1.2.2
|
||||||
|
scipy==1.10.1
|
||||||
|
six==1.16.0
|
||||||
|
tenacity==8.2.2
|
||||||
|
threadpoolctl==3.1.0
|
||||||
|
tzdata==2023.3
|
||||||
|
Werkzeug==2.3.4
|
||||||
@@ -0,0 +1,323 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
from dash import Dash, dcc, html, Input, Output
|
||||||
|
import plotly.express as px
|
||||||
|
import plotly.graph_objects as go
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import dash_bootstrap_components as dbc
|
||||||
|
from sklearn.datasets import make_blobs
|
||||||
|
|
||||||
|
# New: Density heatmap (2 columns) as third plot on tab 2
|
||||||
|
# with color and resolution options
|
||||||
|
# New: Everything with inline style and bootstrap (no CSS)
|
||||||
|
|
||||||
|
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
||||||
|
|
||||||
|
# generate random normal distributed data for x and y
|
||||||
|
# and store it in a Pandas DataFrame (for plot 1,2, and 5)
|
||||||
|
|
||||||
|
np.random.seed(seed=8)
|
||||||
|
|
||||||
|
df = pd.DataFrame(
|
||||||
|
{
|
||||||
|
"y": np.random.normal(loc=0, scale=10, size=1000),
|
||||||
|
"x": np.random.normal(loc=10, scale=2, size=1000),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# define cluster colors
|
||||||
|
|
||||||
|
COLORS = {"0": "red", "1": "blue", "2": "grey"}
|
||||||
|
|
||||||
|
# generic cluster data (for plot 3 and 4)
|
||||||
|
|
||||||
|
X, y = make_blobs(
|
||||||
|
n_samples=7500, centers=3, n_features=2, random_state=0, cluster_std=0.75
|
||||||
|
)
|
||||||
|
|
||||||
|
cluster_df = pd.DataFrame(data=X, columns=["X", "Y"])
|
||||||
|
cluster_df["cluster"] = [str(i) for i in y]
|
||||||
|
|
||||||
|
app.layout = html.Div(
|
||||||
|
[
|
||||||
|
html.Div([html.H1("Dashboard 6")], style={"margin": "10px 25px 25px 25px"}),
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dcc.Tabs(
|
||||||
|
id="tabs",
|
||||||
|
children=[
|
||||||
|
dcc.Tab(
|
||||||
|
label="Tab One",
|
||||||
|
children=[
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Dropdown(
|
||||||
|
options=[
|
||||||
|
"red",
|
||||||
|
"green",
|
||||||
|
"blue",
|
||||||
|
],
|
||||||
|
value="red",
|
||||||
|
id="color",
|
||||||
|
multi=False,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[
|
||||||
|
dcc.Slider(
|
||||||
|
min=math.floor(
|
||||||
|
df["y"].min()
|
||||||
|
),
|
||||||
|
max=math.ceil(
|
||||||
|
df["y"].max()
|
||||||
|
),
|
||||||
|
id="min_value",
|
||||||
|
)
|
||||||
|
],
|
||||||
|
width=6,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_1")], width=6
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_2")], width=6
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
],
|
||||||
|
style={"margin": "100px 25px 25px 25px"},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
dcc.Tab(
|
||||||
|
label="Tab Two",
|
||||||
|
id="tab_2_graphs",
|
||||||
|
children=[
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_3")], width=8
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_4")], width=4
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Label(
|
||||||
|
"Number of bins:",
|
||||||
|
html_for="graph_5_nbins",
|
||||||
|
),
|
||||||
|
dcc.Dropdown(
|
||||||
|
options=[
|
||||||
|
str(i)
|
||||||
|
for i in range(
|
||||||
|
5, 100, 5
|
||||||
|
)
|
||||||
|
],
|
||||||
|
value="40",
|
||||||
|
id="graph_5_nbins",
|
||||||
|
multi=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
width={"size": 3},
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Label(
|
||||||
|
"Color:",
|
||||||
|
html_for="graph_5_color",
|
||||||
|
),
|
||||||
|
dcc.Dropdown(
|
||||||
|
options=[
|
||||||
|
"Viridis",
|
||||||
|
"Magma",
|
||||||
|
"Hot",
|
||||||
|
"GnBu",
|
||||||
|
"Greys",
|
||||||
|
],
|
||||||
|
value="Viridis",
|
||||||
|
id="graph_5_color",
|
||||||
|
multi=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
width={"size": 3, "offset": 1},
|
||||||
|
),
|
||||||
|
dbc.Col(
|
||||||
|
html.Div(
|
||||||
|
[
|
||||||
|
dbc.Label(
|
||||||
|
"Separated for Cluster:",
|
||||||
|
html_for="graph_5_separated",
|
||||||
|
),
|
||||||
|
dcc.RadioItems(
|
||||||
|
options=["Yes", "No"],
|
||||||
|
value="No",
|
||||||
|
id="graph_5_separated",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
width={"size": 3, "offset": 1},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row(
|
||||||
|
[
|
||||||
|
dbc.Col(
|
||||||
|
[dcc.Graph(id="graph_5")], width=12
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
dbc.Row([html.Div(id="html_table")]),
|
||||||
|
],
|
||||||
|
style={"margin": "10px 25px 25px 25px"},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
style={"margin": "10px 25px 25px 25px"},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def update_selected_data(selected_data):
|
||||||
|
if selected_data is None or (
|
||||||
|
isinstance(selected_data, dict) and "xaxis.range[0]" not in selected_data
|
||||||
|
):
|
||||||
|
cluster_dff = cluster_df
|
||||||
|
else:
|
||||||
|
cluster_dff = cluster_df[
|
||||||
|
(cluster_df["X"] >= selected_data.get("xaxis.range[0]"))
|
||||||
|
& (cluster_df["X"] <= selected_data.get("xaxis.range[1]"))
|
||||||
|
& (cluster_df["Y"] >= selected_data.get("yaxis.range[0]"))
|
||||||
|
& (cluster_df["Y"] <= selected_data.get("yaxis.range[1]"))
|
||||||
|
]
|
||||||
|
return cluster_dff
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(Output("graph_1", "figure"), Input("color", "value"))
|
||||||
|
def update_graph_1(dropdown_value_color):
|
||||||
|
fig = px.histogram(df, x="y", color_discrete_sequence=[dropdown_value_color])
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(Output("graph_2", "figure"), Input("min_value", "value"))
|
||||||
|
def update_graph_2(min_value):
|
||||||
|
if min_value:
|
||||||
|
dff = df[df["y"] > min_value]
|
||||||
|
else:
|
||||||
|
dff = df
|
||||||
|
|
||||||
|
fig = px.scatter(dff, x="x", y="y")
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
# Hier passt nun graph_5 als input.
|
||||||
|
@app.callback(
|
||||||
|
Output("graph_3", "figure"),
|
||||||
|
Output("graph_4", "figure"),
|
||||||
|
Output("html_table", "children"),
|
||||||
|
Input("graph_5", "relayoutData"),
|
||||||
|
)
|
||||||
|
def update_graph_3_and_4(selected_data):
|
||||||
|
PLOT_HEIGHT = 400
|
||||||
|
|
||||||
|
cluster_dff = update_selected_data(selected_data=selected_data)
|
||||||
|
|
||||||
|
fig3 = px.scatter(
|
||||||
|
cluster_dff,
|
||||||
|
x="X",
|
||||||
|
y="Y",
|
||||||
|
color="cluster",
|
||||||
|
color_discrete_map=COLORS,
|
||||||
|
category_orders={"cluster": ["0", "1", "2"]},
|
||||||
|
)
|
||||||
|
|
||||||
|
fig3.update_layout(
|
||||||
|
height=PLOT_HEIGHT, template="plotly_white", coloraxis_showscale=False
|
||||||
|
)
|
||||||
|
fig3.update_traces(marker=dict(size=8))
|
||||||
|
|
||||||
|
group_counts = cluster_dff[["cluster", "X"]].groupby("cluster").count()
|
||||||
|
|
||||||
|
fig4 = go.Figure(
|
||||||
|
data=[
|
||||||
|
go.Bar(
|
||||||
|
x=group_counts.index,
|
||||||
|
y=group_counts["X"],
|
||||||
|
marker_color=[COLORS.get(i) for i in group_counts.index],
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
fig4.update_layout(
|
||||||
|
height=PLOT_HEIGHT,
|
||||||
|
template="plotly_white",
|
||||||
|
title="<b>Counts per cluster</b>",
|
||||||
|
xaxis_title="cluster",
|
||||||
|
title_font_size=25,
|
||||||
|
)
|
||||||
|
# dritter return wert: die generierte Tabelle
|
||||||
|
return fig3, fig4, generate_table(group_counts)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_table(dataframe):
|
||||||
|
table = [html.Tr([html.Th("Cluster"), html.Th("Value")])]
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/16476974
|
||||||
|
for index, row in dataframe.iterrows():
|
||||||
|
table.append(html.Tr([html.Td(index), html.Td(row["X"])]))
|
||||||
|
return dbc.Table(table, bordered=True)
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(
|
||||||
|
Output("graph_5", "figure"),
|
||||||
|
Input("graph_5_nbins", "value"),
|
||||||
|
Input("graph_5_color", "value"),
|
||||||
|
Input("graph_5_separated", "value"),
|
||||||
|
Input("graph_3", "relayoutData"),
|
||||||
|
)
|
||||||
|
def update_graph_5(nbins, color, separated, selected_data):
|
||||||
|
cluster_dff = update_selected_data(selected_data=selected_data)
|
||||||
|
|
||||||
|
fig = px.density_heatmap(
|
||||||
|
cluster_dff,
|
||||||
|
x="X",
|
||||||
|
y="Y",
|
||||||
|
nbinsx=int(nbins),
|
||||||
|
nbinsy=int(nbins),
|
||||||
|
color_continuous_scale=color,
|
||||||
|
facet_col=None if separated == "No" else "cluster",
|
||||||
|
category_orders={"cluster": ["0", "1", "2"]},
|
||||||
|
)
|
||||||
|
fig.update_layout(template="plotly_white")
|
||||||
|
return fig
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run_server(debug=True, port=8014)
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
blinker==1.6.2
|
||||||
|
click==8.1.3
|
||||||
|
dash==2.9.3
|
||||||
|
dash-bootstrap-components==1.4.1
|
||||||
|
dash-core-components==2.0.0
|
||||||
|
dash-html-components==2.0.0
|
||||||
|
dash-table==5.0.0
|
||||||
|
Flask==2.3.2
|
||||||
|
itsdangerous==2.1.2
|
||||||
|
Jinja2==3.1.2
|
||||||
|
joblib==1.2.0
|
||||||
|
MarkupSafe==2.1.2
|
||||||
|
numpy==1.24.3
|
||||||
|
packaging==23.1
|
||||||
|
pandas==2.0.1
|
||||||
|
plotly==5.14.1
|
||||||
|
python-dateutil==2.8.2
|
||||||
|
pytz==2023.3
|
||||||
|
scikit-learn==1.2.2
|
||||||
|
scipy==1.10.1
|
||||||
|
six==1.16.0
|
||||||
|
tenacity==8.2.2
|
||||||
|
threadpoolctl==3.1.0
|
||||||
|
tzdata==2023.3
|
||||||
|
Werkzeug==2.3.4
|
||||||
Reference in new issue
Block a user