我有這個用于股票可視化的代碼,任何人都可以幫我找出錯誤我有這個代碼用于我的大學專案并顯示 ValueError: No objects to concatenate 我不知道如何解決這個問題,請有人幫我解決這個問題。圖表已列印,但沒有資料,它也在列印我輸入的股票名稱的 keyerror 并且它也沒有將日期作為引數
import dash
from dash import dcc
from dash import html
from dash import Input
from dash import Output,State
from datetime import datetime as dt
global ticker
app = dash.Dash(__name__)
server = app.server
app.layout = html.Div([html.Div(
[
html.P("Welcome to the Stock Dash App!", className="start"),
html.Br(),
html.Br(),
html.Div([
# stock code input
html.P("Input Stock Code : "),
dcc.Input(placeholder="Enter Stock Name",type='text',value='',id='Stock_code'),
html.Button('Submit',id='submit'),
html.Br(),
html.Br()
]),
html.Div([
# Date range picker input
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=dt(1995, 8, 5),
max_date_allowed=dt(2020, 9, 19),
initial_visible_month=dt(2019, 8, 5),
start_date = dt(2013,4,5),
end_date=dt(2017, 8, 25)
),
html.Br(),
html.Br()
]),
html.Div([
# Stock price button
html.Button('Stock Price',id='price'),
# Indicators button
html.Button('Indicators',id='indicator'),
html.Br(),
html.Br(),
# Number of days of forecast input
dcc.Input(placeholder='Number of Days',type='text',value='',className='Inputs'),
html.Br(),
# Forecast button
html.Button('Forecast',id='forecast')
]),
],className="nav"),
html.Div(
[
html.Div(
[ # Logo
# Company Name
],
className="header",id="Header"),
html.Div( #Description
id="description", className="decription_ticker"),
html.Div([
# Stock price plot
dcc.Graph(id="graph")
], id="graphs-content"),
html.Div([
# Indicator plot
], id="main-content"),
html.Div([
# Forecast plot
], id="forecast-content")],className="content")],
className='container')
#Task 4
import yfinance as yf
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
# @app.callback(
# #Output("component-id-1", "property"),
# Output('description','value'),
# Input('Stock_code','value' ),
# State("Stock_code", "value"))
@app.callback([
Output('description', 'children'),
Output('Header','children'),
# Output('main-content','children')
],
[Input('Stock_code','value')],
[State('submit', "value")]
)
def info_data(value,value2):
#input parameter(s)
#your function here
global ticker
ticker = yf.Ticker(value)
inf = ticker.info
df = pd.DataFrame().from_dict(inf, orient="index").T
logo_url = df["logo_url"]
BusinessSummary = df["longBusinessSummary"]
return logo_url,BusinessSummary
@app.callback([
Output("graphs-content",'children')
# Output("main-content","children")
],
[
Input("Stock_code","value"),
Input("my-date-picker-range","start_date"),
Input("my-date-picker-range","end_date"),
Input("price","value")
]
)
def Graph(ticker1,start_date,end_date,priceValue):
global ticker
df = yf.download(ticker1)
df.reset_index(inplace=True)
fig = get_stock_price_fig(df)
return fig
def get_stock_price_fig(df):
fig = px.line(df,
x= "Year", # Date str,
y = "Open",# list of 'Open' and 'Close',
title="Closing and Opening Price vs Date")
fig.show()
return fig
if __name__ == '__main__':
app.run_server(debug=True)
uj5u.com熱心網友回復:
問題是如何Dash作業的。
當瀏覽器加載頁面時,Dash 會自動運行所有callback- 但此時大多數表單都是空的,因此它運行具有空值的函式(空字串而不是ticker等)。
在回呼中,您必須檢查它是否獲得價值并跳過代碼raise PreventUpdate
檔案:高級回呼
@app.callback([
Output('description', 'children'),
Output('Header','children'),
],
[Input('Stock_code','value')],
[State('submit', "value")]
)
def info_data(value, value2):
global ticker
if value:
ticker = yf.Ticker(value)
inf = ticker.info
df = pd.DataFrame.from_dict(inf, orient="index").T
logo_url = df["logo_url"]
BusinessSummary = df["longBusinessSummary"]
return logo_url,BusinessSummary
else:
raise PreventUpdate
@app.callback([
Output("graphs-content",'children')
],
[
Input("Stock_code","value"),
Input("my-date-picker-range","start_date"),
Input("my-date-picker-range","end_date"),
Input("price","value")
]
)
def Graph(ticker1,start_date,end_date,priceValue):
global ticker
if ticker1:
df = yf.download(ticker1)
df.reset_index(inplace=True)
fig = get_stock_price_fig(df)
return fig
else:
raise PreventUpdate
稍后您可能會遇到其他錯誤,因為您不檢查是否從服務器獲取任何值。例如,如果你寫錯了ticker,那么你就不會得到longBusinessSummary,但有時即使是正確的ticker也可能沒有longBusinessSummary。
問題也可能與Year-Open你必須檢查你是否得到它。
uj5u.com熱心網友回復:
你應該使用streamlit,它更容易。回到你的問題,你應該使用這個
df = pd.DataFrame.from_dict(inf, orient="index").T
代替
df = pd.DataFrame().from_dict(inf, orient="index").T
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444741.html
