(请忽略一些日语单词)
在下面编写这些代码并得到“赋值之前引用了局部变量'Auto_field'”错误
def图表_回报_不同_国家(bq): count_list = ['民生用电気机器', '重电机受注生产品', '产业用泛用'] field_list = ['电気冷蔵库','电気冷蔵库うち401L以上','自动车','建设业','产业用泛用电気机器计','回転・駆动机器计']
input_1 = widgets.Dropdown(options = count_list,
value = '民生用電気機器',
description = 'Choose a country',
style={'description_width': 'initial'},
layout=Layout(width='50%', height='40px')
)
input_2 = widgets.Dropdown(options = field_list,
value='電気冷蔵庫',
description='機器')
start_time = date(2020, 4, 30)
input_3 = widgets.DatePicker(description='Choose start date', disabled=False, value=start_time)
end_time = datetime.today()
input_4 = widgets.DatePicker(description='Choose end date', disabled=False, value=end_time)
民生用電気機器_電気冷蔵庫 ={'House Appliance Refridgerator': 'JNESHQER Index'}
民生用電気機器_電気冷蔵庫うち401L以上 = {'House Appliance Refridgerator Over 401 Litre':'JNESHQRO Index'}
重電機器受注生産品_自動車 = {'Heavy Electric Equipment - Automobiles': 'JPCITOTL Index'}
重電機器受注生産品_建設業= {'Heavy Electric Equipment - Construction':'JNHECONS Index'}
産業用汎用産業用汎用電気機器計 = {'Industrial Equipment Total': 'JEESTOTL Index'}
産業用汎用回転の駆動機器計 = {'Industrial Equipment - Rotating/driving equipment meter' :'JEESMREG Index'}
fig = go.Figure()
#fig.update_layout(barmode = 'stack')
fig.update_traces(textfont_size=18, textangle=0, textposition ="outside", cliponaxis =False)
fig.layout.xaxis.title.text = 'Date'
fig.layout.yaxis.title.font.size=18
fig.update_layout(template ='plotly_dark',height=700)
fig.update_layout(xaxis={'side':'bottom'})
fig.update_xaxes(tickfont_size=18, tickangle=0)
fig.update_yaxes(tickfont_size=18)
fig.update_layout(xaxis=dict(showgrid=False), yaxis=dict(showgrid=False))
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig_w = go.FigureWidget(fig)
def update_chart(evt=None):
global field_0
global data_item
if input_1.value == "民生用電気機器":
Fridge_field = 民生用電気機器_電気冷蔵庫
Fridgebig_field = 民生用電気機器_電気冷蔵庫うち401L以上
elif input_1.value == "重電機器受注生産品":
Auto_field = 重電機器受注生産品_自動車
Construction_field = 重電機器受注生産品_建設業
else:
IETotal_field = 産業用汎用_産業用汎用電気機器計
Rotating_field = 産業用汎用回の転駆動機器計
field_0 = {
'Refridgerator': Fridge_field ,
'Refridgerator Larger than 401 Litre': Fridgebig_field ,
'Automobiles equipment': Auto_field ,
'Construction equipment': Construction_field ,
'Industrial equipment total': IETotal_field ,
'Rotating/driving equipment meter': Rotating_field}
data_item = {'Value': bq.data.px_last(dates=bq.func.range(input_3.value, input_4.value),fill='NA',per='M')} #currency='USD'
selected_index = field_0.get(input_2.value)
global bql_request
bql_request = bql.Request(list(selected_index.values()), data_item)
df = bql.combined_df(bq.execute(bql_request))
df.index = df.index.map(dict(zip(selected_index.values(), selected_index.keys())))
df = df.reset_index()
df = df.pivot(index='DATE', columns='ID', values='Value')
df_plot = df.copy()
df_plot.reset_index(inplace=True)
df_plot.set_index('DATE', inplace=True)
x = df_plot.index
fig_w.data= []
for i in range(len(df_plot.columns.values)):
if i == 0:
fig_w.add_trace(go.Scatter(name= df_plot.columns.values[0], x= x, y= df_plot[df_plot.columns.values[0]], mode='lines', yaxis='y1'), secondary_y=False)
else:
try:
fig_w.add_trace(go.Scatter(name=df_plot.columns.values[i], x= x, y= df_plot[df_plot.columns.values[i]], mode='lines', yaxis='y2'), secondary_y=True)
# fig_w.set_ylabel('Others', color = 'r')
except:
pass
y_label1 = df_plot.columns.values[0] + ' Scale'
y_label2 = 'Others'
fig_w.update_layout(yaxis = dict(title = y_label1), yaxis2 = dict(title = y_label2))
fig_w.layout.xaxis.title.text = 'Date'
fig_w.update_xaxes(tickangle=45)
fig_w.update_layout(template='plotly_dark', height=700, font_family="Arial", legend_font_size=16, font=dict(
family="Arial",
size=18, ))
fig_w.update_layout(xaxis=dict(tickvals=x))
fig_w.for_each_xaxis(lambda x: x.update(showgrid=False))
fig_w.for_each_yaxis(lambda x: x.update(showgrid=False))
update_chart()
input_1.observe(update_chart, names='value', type='change')
input_2.observe(update_chart, names='value', type='change')
input_3.observe(update_chart, names='value', type='change')
input_4.observe(update_chart, names='value', type='change')
bql_request =[str(bql.Request(list(field_0.get('Refridgerator').values()), data_item)),
str(bql.Request(list(field_0.get('Refridgerator Larger than 401 Litre').values()), data_item)),
str(bql.Request(list(field_0.get('Automobiles equipment').values()), data_item)), str(bql.Request(list(field_0.get('Construction equipment').values()), data_item)), str(bql.Request(list(field_0.get('Industrial equipment total').values()), data_item)),
str(bql.Request(list(field_0.get('Rotating/driving equipment meter').values()), data_item))]
return {'Chart': widgets.VBox([input_1, input_2, input_3, input_4, fig_w]), 'BQL Query': '\n'.join(bql_request)}
chart_returns_ different_country(bq)['图表']
如何解决?非常感谢欣赏
其实我不知道该怎么办
Auto_field
仅在input_1.value = "重電機器受注生産品"
时定义,但在所有情况下都会由field_0
调用,因此如果input1.value
具有不同的值,Python将不知道您需要什么变量。当 input_1.value = "民生用電気機器"
时,您会遇到这种情况,但是考虑到 field_0
的定义方式,由于未定义不同的变量,您在所有情况下都会遇到错误。
对其他情况进行类似的更改,但最简单的处理方式是这种情况。不确定它是否会导致错误。
if input_1.value == "民生用電気機器":
field_0 = {
'Refridgerator': 民生用電気機器_電気冷蔵庫 ,
'Refridgerator Larger than 401 Litre': 民生用電気機器_電気冷蔵庫うち401L以上}