Streamlit Stock Price App in Python

A beginners guide to streamlit

Streamlit is an open-source Python library that allows you to create web applications quickly and easily. With Streamlit, you can turn your data analysis and machine learning models into interactive web applications that can be easily shared and accessed by others.

I created a demo app that allows users to graphically visualize the closing stock prices and volumes of a given company. This project demonstrates the basic structure of streamlit apps, how to take input and how to visualize data.

Click here to visit the app

List of companies in the project

(1) Apple
(2) Google
(3) Meta
(4) Amazon
(5) Tesla
(6) Johnson & Johnson
(7) Exxon Mobil Corp

How to Run the Project

[1] Go to the GitHub repository and clone it to your local computer.

[2] Create a virtual environment. Open the command prompt in the same directory and run the following command,

python -m venv venv

This will create a virtual environment for the project. (You can create a virtual environment by any preferred method)

[3] Activate the virtual environment, for that run the below command,

venv\Scripts\activate

[4] Install the requirements of the project.

pip install -r requirements.txt

[5] The project is ready to run. Execute the following command,

streamlit run streamlit-app.py

Your streamlit app will open in a new tab in your browser.

Let's Understand Streamlit

Following are some built-in functions in streamlit,

MethodDescription
streamlit.titleThis method allows you to set the title of your web application.
streamlit.subheaderThis method allows you to add a subheader to your application
streamlit.markdownThis method allows you to write markdown text in your application.
streamlit.text_inputThis widget allows users to input text.
streamlit.number_inputThis widget allows users to input numbers.
streamlight.imageThis method allows you to display images in your application.
streamlit.dataframeThis method allows you to display pandas dataframe in your application.
streamlit.selectboxThis method allows you to create a drop-down box in your application.
streamlit.sliderThis method allows you to create a slider in your application.
streamlit.buttonThis method allows you to create a button in your application.
streamlit.checkboxThis method allows you to create a checkbox in your application.
streamlit.radioThis method allows you to create a radio button in your application.

These are just a few of the many methods available in Streamlit. With these methods, you can build a wide variety of interactive web applications for your data analysis and machine learning projects.

Understanding the Code:

import pandas as pd 
import streamlit as st 
import yfinance as yf
from datetime import date

options = ["AAPL", "GOOGL", "META", "AMZN","TSLA","JNJ","XOM"]

def main():
    # Setting Title
    st.title("Stock Price")

    # Setting Header
    st.subheader("Choose a stock and duration to see graph")

    # Creating a dropdown
    symbol = st.selectbox("Select an option", options)

    # Taking Date Inputs
    startdate = str(st.date_input("Select a start date", date.today()))
    endtdate = str(st.date_input("Select an end date", date.today()))

    # Getting stock Data from yahoo finance
    tickerData = yf.Ticker(symbol)
    tickerDf = tickerData.history(period='1d', start=startdate,end=endtdate)

    submit = st.button("Get Graphs")


    if submit:
        # Ploting Data
        st.markdown("""
        ### Closing Price
        """)
        st.line_chart(tickerDf.Close)

        st.markdown("""
        ### Volume
        """)
        st.line_chart(tickerDf.Volume)
    st.markdown("Developed By Azeem Waqar")
    st.write("""
            [Click here](https://github.com/AzeemWaqarRao/Streamlit-Stock-Price-App) to visit Github Repository.
            """)


if __name__ == "__main__":
    main()

Now let's just understand the code step by step,

  1. First I made all necessary imports.

  2. Then I wrote a main() function where the whole body lies.

  3. I set the Title and subtitle using streamlit.title() and streamlit.subheader() simultaneously.

  4. Then I created a dropdown with streamlit.selectbox(), passed the message to display as 1st parameter, the list of options as 2nd parameter and saved the user's choice in a variable called symbol.

  5. Then I took start and end dates as input using streamlit.input_date() and parse the dates as string

  6. Then I used yahoo finance API to get data of particular symbol between specified dates. The data is returned as a pandas dataframe containing columns like open, close, high, low and volume.

  7. After this I created a button and called it submit . The conditional at the next line makes sure to show the graphs only when the button is clicked.

  8. Then I plotted the graphs of closing value and volume using streamlit.line_chart(). The plots were created against days.

  9. streamlit.write() or streamlit.markdown() lets you write markdown directly as a parameter.

Deploying the streamlit app

First, go to streamlit website and signup.

Deploying your streamlit app is fairly easy. You just have to upload your project to a GitHub repository. Then on your streamlit app on the top right corner you will find a menu button. Click on it then click deploy this app.

Next, you will see this screen.

Choose the right GitHub repository and branch here, then deploy.

Boom! your app is up and running.