定期更新 plotly 中的数字

Periodically updating figures in plotly

下面是一个检索活人口数据的简单脚本。它定期更新并更新绘图:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc


# Retrieve data
link = requests.get("https://countrymeters.info/en/World").text
table = pd.read_html(link)[0]
table = table
figure = table.iplot(asFigure=True, kind='bar',xTitle='Source: World',yTitle='Live Pop',title='Population')

# Dash app
app = dash.Dash(external_stylesheets=[dbc.themes.LUX])

# Bootstrap CSS
app.css.append_css({"external_url": "https://codepen.io/amyoshino/pen/jzXypZ.css"})
link = session.get("https://countrymeters.info/en/World").text

app.layout = html.Div(children=[
    html.H1("Population Data Scrape"),

    html.Div(children=
    '''
    A summary of the latest population data across the globe.
    '''     ),

    # Graph 1
    html.Div([
        dcc.Tabs([ #Tab 1
            dcc.Tab(label="Population Data", children=[

                html.Div([
                    dcc.Graph(
                        id = "Data",
                        figure = table.iplot(asFigure=True, kind='bar',xTitle='Source: World',yTitle='Live Pop',title='Population')
                            ),
                            dcc.Interval(
                                id="4secinterval",
                                interval="4000",
                                n_intervals=0
                            )], className = "six columns"),



                        ]),


                ])
            ])
        ])


# Update graph
@app.callback(Output("Data", "figure"),
            [Input("4secinterval", "n_intervals")])
def draw_figure(n):      
    test = session.get("https://countrymeters.info/en/World").text
    table = pd.read_html(test)[0]
    table = table
    figure = table.iplot(asFigure=True, kind='bar',xTitle='Source: World',yTitle='Live Pop',title='Population')
    return figure

if __name__ == "__main__":
    app.run_server(debug=False)

在我的代码的 "update graph" 部分,为了更新图表,我必须再次调用网络抓取以检索最新数据并将其定义在一个完整的函数中。我试过在使用之前定义函数:

@app.callback(Output("Data", "figure"),
            [Input("4secinterval", "n_intervals")])
draw_figure(n)

我希望只是 return 这个数字,但是,这不起作用。 plotly/Dash 中有没有一种方法可以以更短的方式更新图形(即无需重新抓取和格式化数据)?

这里的关键在于 dcc.Graph 部分。您正在调用全局变量 table.iplot(),其中 table 在检索部分中定义为全局变量。

尝试将所有函数放在一个单独的文件中说`useful_functions.py'

def draw_graph():
  link = requests.get("https://countrymeters.info/en/World").text
  table = pd.read_html(link)[0]
  table = table
  figure = table.iplot(asFigure=True, kind='bar',xTitle='Source: World',yTitle='Live Pop',title='Population')
  return figure
the_graph = draw_graph()

现在,在上面的主文件中,删除 table 和图的全局声明。要显示图表,请在您的图表部分调用 draw_graph() 函数:

 import useful_functions as uf
 <rest of the html app code>
 dcc.Graph(
   id = "graph_id",
   figure = uf.the_graph
),

这将在加载时首次调用图形。现在对于刷新位,callback 看起来像:

@app.callback(Output("Data", "figure"),
        [Input("4secinterval", "n_intervals")])
def draw_figure(n):
    fig = uf.draw_graph()
    return fig