1

I use Dash and I want to display a Table, the html.Layout is like this :

html.Div([
     dash_table.DataTable(
          id="table_infos",
          columns=["Intitulé", "Donnée"]
            )
        ], style={'display': 'inline-block', 'verticalAlign': 'top', 'width': '30%', 'padding':'30px'})

And my callback is like this:

@app.callback(Output('table_infos', 'data'),
                [Input('ville-picker', 'value')])
def update_generales(selected_ville):
    departement = df.loc[df['ville'] == selected_ville]['departement'].iloc[0]
    region = df.loc[df['ville'] == selected_ville]['region'].iloc[0]
    cp = df.loc[df['ville'] == selected_ville]['code postale'].iloc[0]
    code_insee = df.loc[df['ville'] == selected_ville]['code_insee'].iloc[0][-5:]
    ci = df.loc[df['ville'] == selected_ville]['code_insee'].iloc[0][18:]

    habitants = df_demo.loc[df_demo['code_insee'] == ci]['Population'].iloc[0]

    infos = {'Intitulé' : ['Région','Département','Code Postal', 'Code Insee',"Nombre d'habitants"],
            'Donnée' : [region, departement, cp, code_insee, habitants]}

    df_info = pd.DataFrame(infos, columns=['Intitulé', 'Donnée'])

    df_info['Intitulé'] = np.asarray(['Région','Département','Code Postal', 'Code Insee',"Nombre d'habitants"])
    data = df_info.to_dict("rows")

    return data

However, I have an empty Table, but with the same number of rows I wished and 2 columns as I wished, But empty Table.

Someone can help me please !

4
  • Why are you creating a dict of your data, but then putting it into a dataframe, renaming the columns to the same thing, and then setting one of those renamed columns to the same data? Either way, that shouldn't be your issue, but it's weird. You don't show your code for the ville picker. Maybe the issue is there? The table will only be updated when the ville picker has a new value. Commented Jul 11, 2019 at 22:03
  • The columns need to be more than just a list of strings. You need a list of dicts with a name and id value in each dict. Try columns=[{'id': "Intitulé", 'name': "Intitulé"}, {'id': "Donnée", 'name': "Donnée"}] Commented Jul 11, 2019 at 22:44
  • the code with the ville picker is okay because I have other graphs and data who are updating when I change the ville (city) Commented Jul 11, 2019 at 22:46
  • 1
    @coralvanda thank you very much, it works ! Commented Jul 11, 2019 at 22:48

1 Answer 1

1

The problem is the way columns was being defined. It must have both the name and id values, like this:

columns=[{'id': "Intitulé", 'name': "Intitulé"}, {'id': "Donnée", 'name': "Donnée"}]

Docs page for reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.