从另一个函数调用Fig

发布于 2025-01-14 01:10:58 字数 549 浏览 3 评论 0原文

您好,这是我的代码的简单分享,我需要在代码 func_2 中使用 func_1 中的图。 我正在绘制一些数据,并且需要提取Fig以在func_2中调用它,因此可以在dash中使用它作为测试

def func_1():
              fig = make_subplots(specs=[[{"secondary_y": True}]])

def func_2():
              app = Dash(__name__)
              server = app.server
              app.layout = html.Div(children=[
              html.H1(children='Hello Dash'),

              html.Div(children='''
              Dash: A web application framework for your data.
              '''), dcc.Graph(id='example-graph',
 figure=fig)])

hello this is a simple share of my code, i need to use the fig from func_1 in the code func_2.
im ploting some data, and need to extract fig to call it in func_2 so can use it in dash as test

def func_1():
              fig = make_subplots(specs=[[{"secondary_y": True}]])

def func_2():
              app = Dash(__name__)
              server = app.server
              app.layout = html.Div(children=[
              html.H1(children='Hello Dash'),

              html.Div(children='''
              Dash: A web application framework for your data.
              '''), dcc.Graph(id='example-graph',
 figure=fig)])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2025-01-21 01:10:58

在Python中,你不能在函数中定义变量并在其他函数中使用它。您可以在顶部声明它(我不建议这样做),或者您可以返回它(下面给出的代码示例)。

def func_1():
  fig = make_subplots(specs=[[{"secondary_y": True}]])
  return fig
def func_2:
  def func_2():
              app = Dash(__name__)
              server = app.server
              app.layout = html.Div(children=[
              html.H1(children='Hello Dash'),

              html.Div(children='''
              Dash: A web application framework for your data.
              '''), dcc.Graph(id='example-graph',
 figure=func_1())])
#Above, you insert the function as a variable (passing in whatever the function returns)

In Python, you can't define a variable in a function and use it in other ones. You could either declare it at the top, which I wouldn't recommend, or you can return it (code example given below).

def func_1():
  fig = make_subplots(specs=[[{"secondary_y": True}]])
  return fig
def func_2:
  def func_2():
              app = Dash(__name__)
              server = app.server
              app.layout = html.Div(children=[
              html.H1(children='Hello Dash'),

              html.Div(children='''
              Dash: A web application framework for your data.
              '''), dcc.Graph(id='example-graph',
 figure=func_1())])
#Above, you insert the function as a variable (passing in whatever the function returns)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文