Web Apps with Python - Streamlit

Web Apps with Python - Streamlit

(Image by Freepik)

In this article, we'll have a look at the Python library Streamlit. You can use it to build complex web apps and dashboards with dropdowns, sliders, text input, date pickers, etc.

And it's really easy to build and deploy them in the cloud!

(Check out my other posts Web Apps with Python - Dash and Web Apps with Python - Gradio)

Intro

Streamlit is a framework to build web apps purely in Python. The syntax is a lot easier to grasp compared to Dash. It has a very active community of users who build components to extend the powers of Streamlit.

Availability

Streamlit is free to install and use. In a notebook environment, use the magic command '!pip install streamlit' in the first cell.

Example Code

The Streamlit gallery site has a wide range of examples. You can also follow the YouTube channel Data Professor for live coding examples.

Limitations

The Streamlit developers are very responsive to user feedback, so things that were limitations in the past, such as slow response times, no tabs, etc, have been corrected. One limitation that I can think of is the narrow choice of themes for the apps. But that may very well change in the future.

Build in the Cloud

I'm going to break from my previous two blog posts and not use Google Colab this time. That's because it is a bit complicated to test Streamlit apps in Colab. Instead, I'll show you how to create and test Streamlit apps in Deepnote and, of course, in Replit.

DEEPNOTE

Here is the link to my public notebook.

I love Deepnote as a cloud-based Python notebook. This Medium article by Avi Chawla discusses Deepnote and its advantages in detail. The Deepnote team are constantly working on improving their product and they have made it really easy to use Deepnote to build and test Streamlit apps. See my step-by-step guide below.

REPLIT

Here is the link to my public Replit project

Coding Streamlit apps on Replit is also very straightforward. Somebody else already did the hard work of making a step-by-step guide, so I'll just post the link here!

How to build Streamlit apps on Replit by Shruti Agarwal.

I want to add a few points of my own:

  • Instead of installing Streamlit by command line in the 'Shell' on the right-hand pane, you can go to 'Packager' under 'Dock' on the left-hand pane, search for Streamlit and install it by clicking on the plus button. You can see the status of your installation in the 'Packager' tab (don't bother to check the 'Console' on the right-hand pane).

  • You can do the same to install other Python libraries that are required and not yet installed. In my case, it was Plotly and DBnomics. If you do it in this way, you do not need to have the 'requirements.txt' file.

  • As the guide says, run the app by typing 'streamlit run main.py' in the 'Shell' on the right-hand pane. Clicking the 'Run' button at the top will not work in the case of Streamlit apps.

  • You can view the Streamlit app in a new browser window by copying and pasting the '...repl.co' link that Replit generates, or by clicking the 'Open in a new tab' icon on the right-hand pane.

  • When you want the app to stop running, type Control-C in the 'Shell' on the right-hand pane.

Deployment

Deployment of Streamlit apps is very easy.

  • You will need to have your code in a public GitHub repo.

  • The repo should have the Python script in a [filename].py file

  • The repo should also have the 'requirements.txt' file for the required packages

  • And, of course, don't forget 'README.md'!

  • Once your repo is set up, just follow the steps as laid out here.

Here is the link to my app.

You can deploy an unlimited number of public apps for free on Streamlit Community Cloud!

Conclusion

  • Streamlit is an awesome and increasingly powerful way to build elaborate web apps and complex dashboards purely in Python.

  • It is easy to code, easy to test and easy to deploy, all in the cloud.

  • Streamlit can be used with many different charting libraries, unlike Dash which uses only Plotly. Streamlit allows for greater layout customisation compared to Gradio.

  • This makes it my preferred Python web app builder.

The dedication of the developers plus the enthusiasm of the user community means that Streamlit's future as a flexible and customisable Python web app framework is bright. There is an exciting new development called 'stlite' -- read about it here. I plan to try it out properly and report on it in a future blog post!