I'm trying to def a functin but i can't call it:
def test():
fig = make_subplots(rows=1, cols=2, specs=[
[{'type': 'scatter'}, {'type': 'table'}]]
, shared_xaxes=True,
horizontal_spacing=0.05,
vertical_spacing=0.05,
column_widths=[0.7, 0.3])
pass
test()
fig.add_trace(go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
increasing_line_color = UP_CANDLE,
decreasing_line_color = DW_CANDLE),row=1,col=1)
When I call it I retrieve the following error: NameError: name 'fig' is not defined If I remove the function, the code works perfectly.
CodePudding user response:
Once your variable fig
is inside the function, nothing outside of the function test
will have access to fig unless you return fig from the function – you can read about scope in python.
I would rewrite your function like this (and pass
doesn't have any purpose in your function so I removed it):
def test():
fig = make_subplots(
rows=1, cols=2,
specs=[[{'type': 'scatter'}, {'type': 'table'}]],
shared_xaxes=True,
horizontal_spacing=0.05,
vertical_spacing=0.05,
column_widths=[0.7, 0.3]
)
return fig
Then you can set a variable equal to what your function returns:
fig = test()
And then you'll be able to access the .add_trace
method of `fig
fig.add_trace(go.Candlestick(x=df.index,
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close'],
increasing_line_color = UP_CANDLE,
decreasing_line_color = DW_CANDLE),row=1,col=1)