Use of procedures, parameter passing, block structures, data types, arrays, abstract data structures, conditional control, iterative and recursive processes, and input/output in programming solutions to a variety of problems. Prerequisite: None.

Project 3 - Stocks Web Application - (100 points)



Due date: 4/24/19

Objectives



  • Learn to use a stocks API
  • Practice with HTML and Flask
  • Practice with string operations and dictionaries

Introduction



For this project, you will create a web application using Flask that will connect with an API to get live stocks information. We will use the Alpha Vantage API that allows users to get detailed information about any stock.

Part 1 - Obtain and test the API key



  • Install flask using pip3 install flask
  • Install requests to get data from the API using pip3 install requests.
  • Get your API key from the Alpha Vantage website to get live stocks data.
  • Test the API key by using the following Python function where the input parameter (symbol) is a stock symbol such as MSFT for Microsoft or AAPL for Apple:

    import requests 
    
    def getStock(symbol):
        baseURL = 'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&datatype=csv'
        keyPart = '&apikey=' + "YOUR_API_KEY" #Add API key
        symbolPart = '&symbol=' + symbol
        stockResponse = requests.get(baseURL+keyPart+symbolPart)
        return stockResponse.text  #Return only text part of response
    


    For AAPL, at the time of running the program, I get the following output:

    symbol,open,high,low,price,volume,latestDay,previousClose,change,changePercent
    AAPL,199.5400,203.3800,198.6100,203.1300,28906780,2019-04-17,199.2500,3.8800,1.9473%

Part 2 - Build and test the app locally



  • Create a HTML form that contains a textbox for the name of the stock symbol, and checkboxes for the opening price, highest value for the day, lowest value for the day, and the current price.

  • Once the user hits the submit button, the user should see appropriate stock value for the symbol of their choice. Furthermore, depending on the boxes that were checked by the user on the HTML form, the appropriate data should be shown to the user. For example, if the user checked "Opening Price", "Low", and "Current Price", the user should be shown the following:

    Stock value for AAPL is as follows:

    Opening Price: 199.5400

    Low: 198.6100

    Current Price: 203.1300

  • >Error checking - If the user types a symbol that does not exist on the stock market, then your webapp must provide an appropriate error message rather than crashing.
  • Add a Back link - At the bottom of the page that contains the search results, include the following code to take you back to the first page.

    html += '<a href="/">Back</a>'
Hint 1: Use the getStock() function mentioned in Part 1 in the webapp. Use string functions to extract the opening, low, high, or current price from the text returned by the getStock() function.

Hint 2: When using checkboxes, use the request.form.get('variable') function rather than the request.form['variable'] function.

Part 3 - Host the webapp on PythonAnywhere



  • Create an account on PythonAnywhere.com
  • You need to create a basic account and setup a Flask Application.
  • Once you setup your application go to the Files section and then navigate to the mysite directory. In this folder you will find a file called flask_app.py. Paste your webapp code in here.
  • Test your app on PythonAnywhere.com. My simple test app is at http://apjoshi.pythonanywhere.com. Make sure your test app is working at yourusername.pythonanywhere.com.

Extra credit (5 points)



Explore the Alpha Vantage API and incorporate a feature from their API into your webapp. Only attempt this after your webapp is running perfectly on PythonAnywhere.

Submitting the project



Please turn in the following for the project.

  • flask-stocks.py
  • README - This file must contain the link to your webapp on PythonAnywehere.com