Home > Enterprise >  My Code is showing ValueError: No objects to concatenate
My Code is showing ValueError: No objects to concatenate

Time:03-14

I have this code for stock visualizing using can anyone help me out figuring out the error I have this code for my college project and its showing ValueError: No objects to concatenate I dont know how to solve this please someone help me out with this. Graph is printed but without data it is printing also on keyerror is coming for the stock name I am entering And It's also not taking dates as arguments

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) 

CodePudding user response:

You should use streamlit, it is easier. Back to your question, you should use this

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

Instead of

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

CodePudding user response:

Porblem is how Dash works.

When browser loads page then Dash automatically runs all callback - but at this moment most forms are empty so it runs functions with empty values (empty strings instead of ticker, etc).

In callbacks you have to check if it get value and skip code with raise PreventUpdate

Doc: 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

Later you may have other errors because you don't check if you get any values from server. For example if you put wrong ticker then you don't get longBusinessSummary, but sometimes even correct ticker may not have longBusinessSummary.

Problem can be also with Year and Open - you have to check if you get it.

  • Related