Issue
(Please ignore some of the words in Japanese)
Wrote these codes below and got "local variable 'Auto_field' referenced before assignment" error
def chart_returns_different_country(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)['Chart']
How do I fix it? Thanks much with appreciation
I actually don't know what to do
Solution
Auto_field
is only defined in case when input_1.value = "重電機器受注生産品"
, but it is called in all cases by field_0
, so if input1.value
has a different value Python won't know what variable you need. You are hitting the case when input_1.value = "民生用電気機器"
, but given how field_0
is defined you will be hitting errors in all cases because of different variables not being defined.
Make a similar change for other cases, but the most simple way of dealing is this one. Not sure if it causes errors down the road.
if input_1.value == "民生用電気機器":
field_0 = {
'Refridgerator': 民生用電気機器_電気冷蔵庫 ,
'Refridgerator Larger than 401 Litre': 民生用電気機器_電気冷蔵庫うち401L以上}
Answered By - Thoughtful_monkey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.