How to Easily Build a Stock Search Web App in Python

Luigi Bruno
5 min readAug 3, 2020

Building a feature-rich stock search and analysis web app in Python (and some CSS) in less than 120 lines of code

Image from Freepik designed by slidesgo

Introduction

This article builds further upon a story published by Chanin Nantasenamat. It shows you how to easily create a customizable and feature-rich stock search and analysis web app in Python in less than 120 lines of code by leveraging the streamlit and yfinance libraries.

To customize the look and feel of the stock search function of the web app you will also write a few lines of CSS.

For the sake of simplicity, this article shows you how to deploy the web app locally. Feel free to deploy it on the cloud or your web server.

What does the web app do?

The web app will allow you to search for and retrieve information on any stock from Yahoo! Finance using its ticker and display a line chart of the closing prices, the last closing price, and the daily volume. The web app will also allow you to view the following additional information for every stock searched:

  • Stock Actions (i.e. dividends payments and stock splits)
  • Quarterly Financials
  • List of Institutional Shareholders
  • Quarterly Balance Sheet
  • Quarterly Cashflow
  • Quarterly Earnings
  • List of Analysts Recommendations (i.e. buy, sell, hold)

For each searched ticker, the web app will query the yfinance API, which will retrieve the related market data from Yahoo! Finance and save it into a dataframe that streamlit will use as input to display the data.

Let’s get started

The prerequisites

You will first need to install streamlit and yfinance. To do so, I recommend using a virtual python environment so as not to pollute your global python environment.

To install streamlit run:

pip install streamlit

To install yfinance run:

pip install yfinance

You will also need the datetime library which comes with the default python installation, hence no need to install it separately.

The code of the web app

This article presents the code of the web app in two separate parts.

The first is the main part, which contains the code for setting up the web app and the default features. The second part contains the code for the additional features.

Part 1 — The main features

The code is self-explanatory. If you go through it line by line and read the comments you should understand what each line does. Nevertheless, a few clarifications are in order:

  • All features of the web-app will be displayed on the sidebar using the streamlit sidebar method, whereas all outputs will be displayed in the main body.
  • Line 12
    If you copy and paste the code above, you will also need to save the CSS sheet (we will see it later) in the same folder in which you will save the source code of the web app.
  • Lines 15–19
    Unfortunately, the text input method in streamlit cannot be left blank or it will display an empty graph. To avoid this, I have put the “GOOG” ticker as the default search that is performed each time the web app is run.
  • Lines 39–42
    Since markets are closed on weekends and public holidays, I have written an if statement that checks if the dataframe is empty (this may happen, for example, if we wanted the latest closing price of a stock on a Sunday) and, if so, prints that “No data is available at the moment”.
  • To those familiar with markdown, formatting the text to be displayed using streamlit will come naturally. If you are not familiar with markdown, I recommend having a look online as there are excellent markdown formatting guides.

As a general suggestion, I think that it is worthwhile to spend some time going through the official streamlit documentation, especially if you’re interested in customizing this web-app further or in using the library for any other project.

The CSS Sheet

By using a style sheet, you can customize the background and text colour of the web app, the colour and size of the “GO” button, as well as the colour of the default text of the search box in the sidebar (i.e. “Enter a valid stock ticker…” — see line 16 of Part 1 of the code).

Part 2 — The additional features

The code for the second part of the web app is easier to read and understand than the one for the first part. As you can see, each of the 7 blocks of code corresponds to one additional feature. In particular, it should be noted that:

  • All additional features will be displayed as checkboxes on the sidebar below the main search bar. Once a checkbox is ticked, the retrieved information, if available, can then be seen in the main body of the web app.
  • Since for some stocks, not all additional information is available at all times, I have written an if statement like the one we have seen for Lines 39–42 of Part 1.

Let’s run it

First thing first, make sure that both parts of the code shown above are consecutive and written in a single file named webapp.py, as well as that the style.css file is stored in the same folder.

Now open a terminal session, navigate to the folder where the files are stored, and run:

streamlit run webapp.py

If all is proceeding well, you should see a terminal message saying that you can view your streamlit app in your browser. At this point, a new window of your default web browser will open and display your web app at the address http://localhost:8501.

Note that if you run the app on a Monday or the day after a public holiday, you might get this message as well:

- GOOG: No data found for this date range, symbol may be delisted

Nothing to worry about! The web app has queried yfinance to retrieve the closing price for the ticker “GOOG” when the market was closed, and hence it could not find any information. In this case, the app will conveniently display that “No data is available at the moment”.

Once it is running, the web app should look like this:

Screenshot of the web app running locally

Awesome! You now have a running stock search and analysis web app.

Have fun experimenting with it! :)

--

--