简体   繁体   English

在 plotly/dash 中为数据表的数据源返回一个空字典

[英]Returning an empty dictionary for the datasource of a datatable in plotly/dash

My callback function reads a value selected by the user (site name) and then queries data for that site and returns 3 figures and one dictionary (df.to_dict('records') to supply the data for a datatable.我的回调 function 读取用户选择的值(站点名称),然后查询该站点的数据并返回 3 个数字和一个字典(df.to_dict('records')以提供数据表的数据。

If the user selects a site for which there is no data, I return {} .如果用户选择没有数据的站点,我返回{} That seems to break it.这似乎打破了它。 If I select a site, the data table fills in properly, switch to another site, same thing.如果我 select 一个站点,数据表填写正确,切换到另一个站点,同样的事情。 Once I select a site with no data, the data table will no longer update, no matter which site I select.一旦我 select 一个没有数据的站点,数据表将不再更新,无论我在哪个站点 select。

Some relevant code:一些相关代码:

The output is defined as: output 定义为:

Output('emission_table','data'),

The return from the callback is:回调的返回是:

return time_series_figure,emissions_df.to_dict('records'),site_map,hotspot_figure
html.Div(style={'float':'left','padding':'5px','width':'49%'}, children = [
    dash_table.DataTable(id='emission_table', data=[],columns=[
    #    {'id': "site", 'name': "Site"},
        {'id': "dateformatted", 'name': "date"}, 
        {'id': "device", 'name': "device"}, 
        {'id': "emission", 'name': "Emission"},
        {'id': "methane", 'name': "CH4"},
        {'id': "wdir", 'name': "WDIR"},
        {'id': "wspd", 'name': "WSPD"},
        {'id': "wd_std", 'name': "WVAR"}],
    #    {'id': "url", 'name':'(Link for Google Maps)','presentation':'markdown'}],
    fixed_rows={'headers': True},
    row_selectable='multi',
    style_table={'height': '500px', 'overflowY': 'auto'},
    style_cell={'textAlign': 'left'})

]),

Any ideas what is happening?任何想法发生了什么? Is there a better way for the callback to return an empty data source for the datatable?回调是否有更好的方法为数据表返回空数据源?

Thanks!谢谢!

You haven't shared enough of your code (your callback specifically) to see what is happening exactly, however:但是,您没有共享足够多的代码(特别是您的回调)来查看到底发生了什么:

If the user selects a site for which there is no data, I return {}如果用户选择没有数据的站点,我返回 {}

is at least one reason why it doesn't work.至少是它不起作用的一个原因。 The data property of a Dash Datatable needs to be a list and not a dictionary. Dash Datatable 的data属性必须是列表而不是字典。 You can however put dictionaries inside the list.但是,您可以将字典放入列表中。 Each dictionary inside the list corresponds to a row in the Data Table.列表中的每个字典对应于数据表中的一行。

So to re-iterate and more directly answer your question:因此,要重新迭代并更直接地回答您的问题:

Is there a better way for the callback to return an empty data source for the datatable?回调是否有更好的方法为数据表返回空数据源?

Yes you can return a list with any number of dictionaries inside.是的,您可以返回一个包含任意数量字典的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM