I’m new using Dash and I wonder if it is possible to have a Dash table with 1 column of numeric values like this one:
JavaScript
x
7
1
Values
2
-------
3
1
4
2
5
3
6
4
7
And have the option to choose/click to one of the values and make a bar plot appear with the value clicked.
Hope you can help me. Thanks in advance.
Advertisement
Answer
You could use the active_cell
property of dash_table.DataTable
to get the clicked value in a callback. Then you can use this value to plot the graph:
JavaScript
1
57
57
1
import pandas as pd
2
from dash import Dash
3
import dash_core_components as dcc
4
import dash_html_components as html
5
from dash.dependencies import Input, Output
6
from dash_table import DataTable
7
import plotly.graph_objects as go
8
9
df = pd.DataFrame(
10
{"values": [1, 2, 3, 4], "labels": ["value 1", "value 2", "value 3", "value 4"]}
11
)
12
13
app = Dash(__name__)
14
15
app.layout = html.Div(
16
[
17
dcc.Graph(id="graph"),
18
DataTable(
19
id="table",
20
columns=[{"name": "values", "id": "values"}],
21
data=df.to_dict("records"),
22
),
23
]
24
)
25
26
27
@app.callback(
28
Output("graph", "figure"), Input("table", "active_cell"), prevent_initial_call=True
29
)
30
def update_output_div(active_cell):
31
selected_value = df.iloc[active_cell["row"], active_cell["column"]]
32
num_values = len(df["values"])
33
34
fig = go.Figure(go.Bar(x=[selected_value], y=[selected_value]))
35
fig.update_layout(yaxis_range=[0, num_values])
36
37
fig.update_layout(
38
yaxis=dict(
39
tickmode="array",
40
tickvals=df["values"],
41
ticktext=df["labels"],
42
),
43
)
44
fig.update_layout(
45
xaxis=dict(
46
tickmode="array",
47
tickvals=[selected_value],
48
ticktext=[selected_value],
49
)
50
)
51
52
return fig
53
54
55
if __name__ == "__main__":
56
app.run_server()
57