Hello there,
I am building a webapp that connects to an API. Currently I have the problem that when I try to make an input I get error messages containing a valueError after the first search which bugs my entire app. Can somebody help me debugging my code? I would love to type an input and enable a correct fillup of my dropdown, which lets the user pick a stock. Also is there a possbility to fill the Dropdown with the “2. name” attribute and the “1. symbol” attribute from the output.json array?
My Code looks like this:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, State, Input
import requests
import dash_bootstrap_components as dbc
from dash import Input, Output, html
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
app = Dash(__name__)
app = Dash(__name__)
app.layout = html.Div([
html.Div([
"Single dynamic Dropdown",
dcc.Dropdown(id ="my-dynamic-dropdown")
]),
])
@app.callback(
Output("my-dynamic-dropdown", "options"),
Input("my-dynamic-dropdown", "search_value"),
)
def update_options(search_value):
if not search_value:
raise PreventUpdate
url = 'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords='+search_value+'&apikey=DEMO'
r = requests.get(url)
data = r.json()
Choices = [i["2. name"] for i in data["bestMatches"]]
return Choices
if __name__ == "__main__":
app.run_server(debug=True)
The Json.array looks like this:
{
"bestMatches": [
{
"1. symbol": "TSCO.LON",
"2. name": "Tesco PLC",
"3. type": "Equity",
"4. region": "United Kingdom",
"5. marketOpen": "08:00",
"6. marketClose": "16:30",
"7. timezone": "UTC+01",
"8. currency": "GBX",
"9. matchScore": "0.7273"
},
{
"1. symbol": "TSCDF",
"2. name": "Tesco plc",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-04",
"8. currency": "USD",
"9. matchScore": "0.7143"
},
{
"1. symbol": "TSCDY",
"2. name": "Tesco plc",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-04",
"8. currency": "USD",
"9. matchScore": "0.7143"
},
{
"1. symbol": "TCO2.FRK",
"2. name": "TESCO PLC ADR/1 LS-05",
"3. type": "Equity",
"4. region": "Frankfurt",
"5. marketOpen": "08:00",
"6. marketClose": "20:00",
"7. timezone": "UTC+02",
"8. currency": "EUR",
"9. matchScore": "0.5455"
},
{
"1. symbol": "TCO0.FRK",
"2. name": "TESCO PLC LS-0633333",
"3. type": "Equity",
"4. region": "Frankfurt",
"5. marketOpen": "08:00",
"6. marketClose": "20:00",
"7. timezone": "UTC+02",
"8. currency": "EUR",
"9. matchScore": "0.5455"
}
]
}
Any help is appreciated. Thank you.