Hello everyone,
I’m a newbie here and I’m glag to start using Dash. I have some knwoledge in Python and Pandas. For my first dashboard, I’m using a French database about food. I just want to:
1- Display a table (dataframe)
2- Make a selection on one or several columns using a dropdown tool and update the table according to this selection.
I looked at the Dash tutorial and guide so as some posts on this forum but I just don’t understand what is happening with my program. I don’t understand the issue and I need some help. I got a Callback error updating data_table.
I’m also not sure to understand how to write the callback. I apparently need to create a def in it but I don’t understand when the parameters I create in this definition are called and how they are linked to my dropdown choices.
I also read somewhere that sometimes we need several callbacks. But in some graph examples I saw in the data guide, they were using only one callback.
Here’s my code. Could you give me some advices?
Thanks so much!
Alpy
# =============================================================================
# Initialization
# =============================================================================
# Packages
# --------
# Pandas
import pandas as pd
# Numpy
import numpy as np
# Functools
from functools import reduce
# Datetime timedelta
from datetime import timedelta
import os
import datetime
import sys
import random
# Plotly Dash
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output
from dash_table import DataTable
from dash.exceptions import PreventUpdate
# Plotly
import plotly.express as px
# =============================================================================
# Data importation
# =============================================================================
# AGRIBALYSE.xlsx
agribalyse = pd.ExcelFile (r'E:\Programmation\Python\Agribalyse\AGRIBALYSE.xlsx')
# Detail ingrédient sheet
tmp_ingredients = pd.read_excel(agribalyse, r'Detail ingredient')
# =============================================================================
# Data management
# =============================================================================
# Abribalyse database with fill-in of all missing rows for products
ingredients=tmp_ingredients.fillna(method='ffill')
# List of columns to display in the application
ingredients_tab_data=ingredients[["Nom Français", "Groupe d'aliment", "Sous-groupe d'aliment", "Ingredients"]]
# List of available products
available_produtcs = ingredients['Nom Français'].unique()
# =============================================================================
# Dash application
# =============================================================================
# Style sheet
# -----------
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# Application call
# ----------------
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Div([
html.H1("Agribalyse"),
html.Div('''
This is a database about food and ecology. The table below shows the products and ingredients.
'''),
html.Div('')
]),
html.Div([
dcc.Dropdown(
id='select_column',
options=[{'label':i, 'value': i} for i in available_produtcs],
value='Pizza jambon fromage',
#multi=True
)
]),
dash_table.DataTable(
id='data_table',
columns=[{"name": i, "id": i} for i in ingredients_tab_data.columns],
data=ingredients_tab_data.to_dict("rows")
)
])
@app.callback(
Output('data_table', 'data'),
Input('select_column', 'value')
)
def update_table(value):
data=ingredients_tab_data[ingredients_tab_data["Nom Français"] == value]
return data
if __name__ == '__main__':
app.run_server(debug=True)