My Code is showing ValueError: No objects to concatenate

My Code is showing ValueError: No objects to concatenate

我有这个股票可视化代码,谁能帮我找出错误 我的大学项目有这段代码,它显示 ValueError: No objects to concatenate 我不知道如何解决这个问题,请有人帮我解决这个问题。图表已打印,但没有数据,它也在打印时出现键错误是我输入的股票名称,而且它也没有将日期作为参数

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) 

你应该使用streamlit, it is easier. Back to your question, you should use this

df = pd.DataFrame.from_dict(inf, orient="index").T

而不是

df = pd.DataFrame().from_dict(inf, orient="index").T

问题是 Dash 的工作方式。

当浏览器加载页面时,Dash 会自动运行所有 callback - 但此时大多数表单都是空的,因此它运行具有空值的函数(空字符串而不是 ticker,等等)。

在回调中,您必须检查它是否获得值并使用 raise PreventUpdate

跳过代码

文档:Advanced Callbacks


@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.

YearOpen 也可能有问题 - 您必须检查是否明白。