簡體   English   中英

有沒有辦法直接從回調中將現有的 dash_table.DataTable 導出為圖像(例如 png、jpg)?

[英]Is there a way to export an existing dash_table.DataTable as an image (e.g. png, jpg) directly from the Callback?

有沒有辦法直接從回調中將現有的 dash_table.DataTable 導出為圖像(例如 png、jpg)? 我試過 fig.savefig 但這在這里不起作用。 有什么想法/方向嗎? 請參閱下面我當前的回調,它將數據返回到 Dash 應用程序中漂亮的 DataTable 中,但我也希望能夠將其導出為圖像。

先感謝您!

# CALLBACK to return input value
@app.callback(
    Output(component_id='output_report_part4', component_property='children'),
    Input(component_id='advice-dropdown', component_property='value'),
    Input(component_id='expanded-advice-text', component_property='value'),  
)
def update_output_div4(advice, expanded_advice):
    df = {'Metric': ['Advice to purchase the dataset:',
                     'Conclusion with expanded advice:'], 
         'Response': [advice, expanded_advice]}   
    # Create DataFrame  
    df = pd.DataFrame(df)
    fig = dash_table.DataTable(df.to_dict('records'), [{"name": i, "id": i} for i in df.columns],
#                                style_cell={'textAlign': 'left'},
                                style_header={'backgroundColor': 'rgb(39,64,139)',
                                              'color': 'white'},
                                style_data={'whiteSpace': 'normal',
                                            'height': 'auto'},
                                style_cell_conditional=[
                                            {
                                                'if': {'column_id': c},
                                                'textAlign': 'left'
                                            } for c in ['Metric', 'Data Vendor']
                                                     ],
                                style_cell={
                                            'height': 'auto',
                                            # all three widths are needed
                                            'minWidth': '90px', 'width': '90px', 'maxWidth': '90px',
                                            'whiteSpace': 'normal'
                                            }
                                )
    return fig

如果 dash webserver 正在運行,您可以使用此 package https://pypi.org/project/html2image/將 url 轉換為圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM