第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何正確地為 Dash Web 應(yīng)用程序執(zhí)行回調(diào)和函數(shù)

如何正確地為 Dash Web 應(yīng)用程序執(zhí)行回調(diào)和函數(shù)

POPMUISE 2022-10-06 16:13:51
非常感謝有關(guān)如何為我的 Dash Web 應(yīng)用程序進(jìn)行正確回調(diào)的一些幫助。我在下面有以下代碼。它應(yīng)該返回一個(gè)圖表,其中包含有關(guān)股票財(cái)務(wù)狀況的各種線(xiàn)條。它從 API 獲取數(shù)據(jù)。但我不確定如何為應(yīng)用回調(diào)定義我的函數(shù)。我嘗試過(guò)遵循在線(xiàn)教程,但取得了任何成功。如果它太混亂或效率低下,我深表歉意,我是新手。'''#!/usr/bin/env pythonimport pandas as pdimport requestsimport jsonimport plotlyimport chart_studio.plotly as pyimport plotly.graph_objs as goimport plotly.express as pximport dashimport dash_core_components as dccimport dash_html_components as htmlfrom dash.dependencies import Output, Inputticker = input("Insert company ticker: ")qoy = input("QUARTERLY or YEARLY: ")if qoy == "Yearly" or qoy == "yearly" or qoy == "YEARLY":    IS = requests.get("https://financialmodelingprep.com/api/v3/financials/income-statement/" + ticker)elif qoy == "Quarterly" or qoy == "quarterly" or qoy == "QUARTERLY":    IS = requests.get(        "https://financialmodelingprep.com/api/v3/financials/income-statement/" + ticker + "?period=quarter")IS = IS.json()IS = IS['financials']IS = pd.DataFrame.from_dict(IS)IS = IS.set_index("date")if qoy == "Yearly" or qoy == "yearly" or qoy == "YEARLY":    BS = requests.get("https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/" + ticker)elif qoy == "Quarterly" or qoy == "quarterly" or qoy == "QUARTERLY":    BS = requests.get(        "https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/" + ticker + "?period=quarter")BS = BS.json()BS = BS['financials']BS = pd.DataFrame.from_dict(BS)BS = BS.set_index("date")if qoy == "Yearly" or qoy == "yearly" or qoy == "YEARLY":    CF = requests.get("https://financialmodelingprep.com/api/v3/financials/cash-flow-statement/" + ticker)elif qoy == "Quarterly" or qoy == "quarterly" or qoy == "QUARTERLY":    CF = requests.get(        "https://financialmodelingprep.com/api/v3/financials/cash-flow-statement/" + ticker + "?period=quarter")CF = CF.json()CF = CF['financials']CF = pd.DataFrame.from_dict(CF)CF = CF.set_index("date")df_FS = pd.concat([IS, BS, CF], axis=1, sort=True)Date = df_FS.indexdf_FS.fillna(0, inplace=True)print(df_FS)
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

您必須在回調(diào)中執(zhí)行所有數(shù)據(jù)處理,因?yàn)樗鼈冃枰斎牒桶粹o值。有多種方法可以解決此問(wèn)題,具體取決于您希望應(yīng)用程序如何響應(yīng)輸入。一種方法是這樣。在這里,主要輸入是qoy,回調(diào)使用stock input. 因此,在您選擇 之前,輸入股票輸入不會(huì)更新應(yīng)用程序qoy。


#!/usr/bin/env python

from plotly.subplots import make_subplots

import pandas as pd

import requests

import json

import plotly

import chart_studio.plotly as py

import plotly.graph_objs as go

import plotly.express as px

import dash

import dash_core_components as dcc

import dash_html_components as html

from dash.dependencies import Output, Input, State

from dash.exceptions import PreventUpdate


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']


app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


app.layout = html.Div([

    # Input the stock ticker

    html.Div([

        dcc.Input(id="stock-input",

                  placeholder='Please insert stock', type="text"),

        dcc.RadioItems(

            id="quarterlyoryearly",

            options=[

                {'label': 'Quarterly', 'value': 'quarterly'},

                {'label': 'Yearly', 'value': 'yearly'}

            ]

        )

    ]),



    # Banner of app

    html.Div([

        html.H2("Stock App")

    ], className="banner"),


    # Graphs

    html.Div([


        # Earnings & Revenue Graph

        html.Div([

            dcc.Graph(

                id="Earnings & Revenue",

                # figure=fig

            )

        ], className="six columns"),

    ], className="row")


])


@app.callback(Output("quarterlyoryearly", "value"),

              [Input("stock-input", "n_submit")],

              [State("quarterlyoryearly", "value")])

def enter_key(n_sub, qoy):

    if n_sub:

        return qoy


@app.callback(dash.dependencies.Output("Earnings & Revenue", "figure"),

              [dash.dependencies.Input("quarterlyoryearly", "value")],

              [dash.dependencies.State("stock-input", "value")]

              )

def update_fig(*args):


    if not any(args):

        raise PreventUpdate

    else:

        qoy, ticker = args


        if qoy.lower() == "yearly":

            IS = requests.get(

                "https://financialmodelingprep.com/api/v3/financials/income-statement/" + ticker)

        elif qoy.lower() == "quarterly":

            IS = requests.get(

                "https://financialmodelingprep.com/api/v3/financials/income-statement/" + ticker + "?period=quarter")


        IS = IS.json()

        IS = IS['financials']

        IS = pd.DataFrame.from_dict(IS)

        IS = IS.set_index("date")


        if qoy == "Yearly" or qoy == "yearly" or qoy == "YEARLY":

            BS = requests.get(

                "https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/" + ticker)

        elif qoy == "Quarterly" or qoy == "quarterly" or qoy == "QUARTERLY":

            BS = requests.get(

                "https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/" + ticker + "?period=quarter")


        BS = BS.json()

        BS = BS['financials']

        BS = pd.DataFrame.from_dict(BS)

        BS = BS.set_index("date")


        if qoy == "Yearly" or qoy == "yearly" or qoy == "YEARLY":

            CF = requests.get(

                "https://financialmodelingprep.com/api/v3/financials/cash-flow-statement/" + ticker)

        elif qoy == "Quarterly" or qoy == "quarterly" or qoy == "QUARTERLY":

            CF = requests.get(

                "https://financialmodelingprep.com/api/v3/financials/cash-flow-statement/" + ticker + "?period=quarter")


        CF = CF.json()

        CF = CF['financials']

        CF = pd.DataFrame.from_dict(CF)

        CF = CF.set_index("date")


        df_FS = pd.concat([IS, BS, CF], axis=1, sort=True)

        Date = df_FS.index

        df_FS.fillna(0, inplace=True)


        # EARNINGS & REVENUE

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

        fig.add_trace(go.Scatter(x=Date, y=df_FS['Revenue'],

                                 mode='lines+markers',

                                 name='Revenue'), secondary_y=False, )

        fig.add_trace(go.Bar(x=Date, y=df_FS['Profit Margin'],

                             opacity=0.2,

                             name='Profit Margin'), secondary_y=True, )


        fig.add_trace(go.Scatter(x=Date, y=df_FS['Consolidated Income'],

                                 mode='lines+markers',

                                 name='Earnings'), secondary_y=False, )

        fig.add_trace(go.Scatter(x=Date, y=df_FS['Operating Cash Flow'],

                                 mode='lines+markers',

                                 name='Operating Cash Flow'), secondary_y=False, )

        fig.add_trace(go.Scatter(x=Date, y=df_FS['Free Cash Flow'],

                                 mode='lines+markers',

                                 name='Free Cash Flow'), secondary_y=False, )

        fig.add_trace(go.Scatter(x=Date, y=df_FS['Operating Expenses'],

                                 mode='lines+markers',

                                 name='Operating Expenses'), secondary_y=False, )


        fig.update_layout(title="EARNINGS & REVENUE",

                          barmode='group', hovermode='x')

        fig.update_yaxes(title_text="in USD", secondary_y=False)

        fig.update_yaxes(title_text="Profit Margin", secondary_y=True)

        fig.update_xaxes(rangeslider_visible=True)

        return fig



if __name__ == '__main__':

    app.run_server(debug=False)

你可以玩轉(zhuǎn)輸入和狀態(tài)選項(xiàng),看看應(yīng)用程序是如何響應(yīng)的。干杯!


PS:我添加了另一個(gè)回調(diào),所以在第一個(gè)qoy和ticker值之后,您可以簡(jiǎn)單地更改ticker輸入字段并點(diǎn)擊輸入按鈕(因?yàn)閝oy已經(jīng)選擇了)來(lái)更新您的應(yīng)用程序。希望這能讓您更深入地了解回調(diào)是如何工作的。


查看完整回答
反對(duì) 回復(fù) 2022-10-06
  • 1 回答
  • 0 關(guān)注
  • 99 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)