【问题标题】:How do I return a dataframe from an app.callback with my as output as dbc.Table.from_dataframe如何从 app.callback 返回数据帧,并将我的输出作为 dbc.Table.from_dataframe
【发布时间】:2019-08-03 15:39:04
【问题描述】:

我试图在单击搜索按钮时加载表格,并提供 ID/密码。我的输出是一个允许 df 参数的 dbc.Table.from_dataframe;但是,当我将其用作输出属性时,出现错误。

以下是“my_table”中的可用属性: ['children', 'id', 'style', 'className', 'key', 'tag', 'size', 'bordered', 'borderless', 'striped', 'dark', 'hover', '响应式', 'loading_state']

我在这里阅读了文档 https://dash-bootstrap-components.opensource.faculty.ai/l/components/table

我尝试过使用“儿童”,但也没有用。我知道使用 dcc 表我需要返回一个字典,但是我认为使用 dbc.Table.from_dataframe 我可以返回一个数据帧。

@app.callback(Output('my_table', 'df' ),
               [Input('search-button','n_clicks')],
               [State('input-id', 'value'),
               State('input-password', 'value')]
           )
def search_fi(n_clicks, iuser, ipasw):
    if n_clicks > 0:

        df = pd.DataFrame(
         {
             "First Name": ["Arthur", "Ford", "Zaphod", "Trillian"],
             "Last Name": ["Dent", "Prefect", "Beeblebrox", "Astra"],
         }
        return df

【问题讨论】:

  • 我之前没有尝试过使用dbc 组件,但是您可以从可用属性中看到df 是不允许的。输出到children 时出了什么问题?作为替代方案,您可以输出到divchildren,您的函数可以return dbc.Table.from_dataframe(df)

标签: python twitter-bootstrap plotly-dash


【解决方案1】:
import dash
import dash_html_components as html
import dash_bootstrap_components as dbc
import pandas as pd

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Button('Table', id='table-but', n_clicks=0),
    html.Div(id='container-button-basic')
])


@app.callback(
    dash.dependencies.Output('container-button-basic', 'children'),
    [dash.dependencies.Input('table-but', 'n_clicks')])

def search_fi(n_clicks):
    if n_clicks > 0:
        df = pd.DataFrame(
         {
             "First Name": ["Arthur", "Ford", "Zaphod", "Trillian"],
             "Last Name": ["Dent", "Prefect", "Beeblebrox", "Astra"]
         })
        print(df)
        return dbc.Table.from_dataframe(df)


if __name__ == '__main__':
    app.run_server(debug=True)

Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多