I have a datatable and I would like to create a pop-up like a dash bootstrap Modal when clicking on it:
The original code is:
import dash
import dash_table
import os
import pandas as pd
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DATA_PATH = os.path.join(PROJECT_ROOT, '../data/')
df = pd.read_csv(DATA_PATH + 'tickers_september_2017_red.csv')
app = dash.Dash(__name__)
def layout():
return dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
)
if __name__ == '__main__':
app.run_server(debug=False, port=8051)
My attempt is:
import dash
import dash_table
import os
import pandas as pd
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_bootstrap_components as dbc
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DATA_PATH = os.path.join(PROJECT_ROOT, '../data/')
df = pd.read_csv(DATA_PATH + 'tickers_september_2017_red.csv')
app = dash.Dash(__name__)
@app.callback(
Output('table', 'children'),
[Input('table', 'active_cell')])
def update_graphs(active_cell):
active_row_id = active_cell['row_id'] if active_cell else None
modal = html.Div(
[
dbc.Button("Open modal", id="open"),
dbc.Modal(
[
dbc.ModalHeader("Header"),
dbc.ModalBody("This is the content of the modal"),
dbc.ModalFooter(
dbc.Button("Close", id="close", className="ml-auto")
),
],
id="modal",
),
]
)
return modal
def layout():
return dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
)
if __name__ == '__main__':
app.run_server(debug=False, port=8051)
However I get:
C:\ProgramData\Anaconda3\envs\homework3\python.exe C:/Users/antoi/Documents/Programming/portfolio-advisor/run.py
Traceback (most recent call last):
File "C:/Users/antoi/Documents/Programming/portfolio-advisor/run.py", line 1, in <module>
from dashboard.app import app #, auth
File "C:\Users\antoi\Documents\Programming\portfolio-advisor\dashboard\app.py", line 8, in <module>
from .pages import header, imap, stocks, markets, sto
File "C:\Users\antoi\Documents\Programming\portfolio-advisor\dashboard\pages\sto.py", line 19, in <module>
[Input('table', 'active_cell')])
File "C:\ProgramData\Anaconda3\envs\homework3\lib\site-packages\dash\dash.py", line 1317, in callback
self._validate_callback(output, inputs, state)
File "C:\ProgramData\Anaconda3\envs\homework3\lib\site-packages\dash\dash.py", line 887, in _validate_callback
"""
dash.exceptions.LayoutIsNotDefined:
Attempting to assign a callback to the application but
the `layout` property has not been assigned.
Assign the `layout` property before assigning callbacks.
Alternatively, suppress this warning by setting
`suppress_callback_exceptions=True`
Process finished with exit code 1