Get Input from html form in Flask

44 views Asked by At

I want to use the alpha vantage api to access financial data of a company via the stock id of a company. I'm trying to get the financial data of a company that the user inputs via the id of the company. In the html file which is called home.html

<form method="post">
<label for="company_id">Enter Company ID (e.g., stock symbol):</label>
<input type="text" name="stock_id" id="sys" placeholder="e.g., AAPL">
<button type="submit">Get Stock Data</button>

The python file which handles calling the api and displaying the data is called views.py

@views.route('/home', methods = [ 'GET' ,'POST'])
def get_stock_data():
    symbol = request.form.get('stock_id')
    api_key = 'hidden'   
    interval = '60min'  
    outputsize = 'compact'  # You can use 'compact' or 'full' for the amount of data
    url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval={interval}&outputsize={outputsize}&apikey={api_key}'
    # Make the HTTP GET request
    response = requests.get(url)
    # Check if the request was successful (status code 200)
    data = response.json()
    time_series = data['Time Series ({})'.format(interval)]

        # Create a Pandas DataFrame from the time series data
    df = pd.DataFrame(time_series).T  # Transpose to have timestamps as index

        # Display the Pandas DataFrame
    print(df)

    df['4. close'] = pd.to_numeric(df['4. close'])
    return render_template("home.html",  symbol=symbol, stock_data=df)
0

There are 0 answers