Python 变量的回调?

Python 变量的回调?,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我有以下代码: # Import Libraries import pandas as pd import numpy as np import plotly.express as px import plotly.graph_objects as go import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input,

我有以下代码:

# Import Libraries

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import dash 
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import datetime 

import dash_bootstrap_components as dbc

date_cols = ["date"]
df = pd.read_csv('base.csv', parse_dates=date_cols)

fig=px.bar(df,x='date',y='value_likes',color='type', barmode='group', 
        color_discrete_sequence=px.colors.sequential.deep, template='plotly_white'
        )

likes = df['value_likes'].iloc[-1]


app = dash.Dash(__name__,external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
app.title = "CSV"

app.layout = html.Div([
  dcc.Interval(
            id='interval-component',
            interval=10*1000, # in milliseconds
            n_intervals=0
        ),
  dbc.Container([
      html.H1("REALTIME MONITORING"),
      html.H4("%s" % likes),
      dcc.Interval(id="progress-interval", n_intervals=0, interval=500),
      dcc.Graph(id='graph', figure=fig)
  ],)

])

@app.callback(
    Output('graph', 'figure'),
    [Input('interval-component', "n_intervals")]
)

def streamFig3(value):
    
    global df
    
    dfglobal = pd.read_csv('base.csv',dtype={'date':'string'})
    likes = dfglobal['value_likes'].iloc[-1]
    fig=px.bar(dfglobal,x='date',y='value_likes',color='type', barmode='group',
        color_discrete_sequence=px.colors.sequential.deep_r, template='plotly_white'
        )

    return(fig)

if __name__ == "__main__":
    app.run_server(debug=True)
此代码每10秒读取一次csv,并相应地更新条形图。 但是它不会更新变量likes,该变量在第一次加载后保持静态。 是否有方法进行回调,以便喜欢也进行更新? 提前感谢您。

解决方案是

@app.callback(
    Output('graph', 'figure'),
    Output('likes-display', 'children'),
    [Input('interval-component', "n_intervals")]
)

def streamFig3(value):

    global df
    
    dfglobal = pd.read_csv('base.csv',dtype={'date':'string'})
    likes=dfglobal['value_likes'].iloc[-1]
    fig=px.bar(dfglobal,x='date',y='value_likes',color='type', barmode='group',
        color_discrete_sequence=px.colors.sequential.deep_r, template='plotly_white'
        )

    return fig, str(likes)
并且还添加到
dbc.container

html.H4(id='likes-display'),
这将允许likes变量在回调内每n秒更新一次


MichelH的回答是

尝试在streamFig3中添加喜欢作为全局变量。@VascoLudovico不太确定我是否理解你的意思,如果我错了,请纠正我,但在我看来,你希望streamFig3中的“喜欢”变量是全局变量。它只是一个局部变量。@VascoLudovico adding
def streamFig3(value):global df dfglobal=pd.read_csv('base.csv',dtype={'date':'string'})fig=px.bar(dfglobal,x='date',y='value\u likes',color='type',barmode='group',color\u discrete\u sequence=px.colors.sequential.deep\r,template='plotly\u white')返回(图)global likes=dfglobal['value\u likes'].iloc[-1]返回(likes)
在csv更新时不会更改likes。我做错了什么?很难在注释中读取未格式化的代码,但我的建议是在streamFig3中的全局变量定义中添加likes,例如
global df,likes