This blog post is a quick summary of the book “Web Application Development with Streamlit”

It has been quite a while since I developed an app on Streamlit, and this week, there was a need to develop one. My knowledge was a bit rusty, and I was not in a plug-and-play mode. I picked up this book as a quick refresher on Streamlit concepts. There have also been many updates in Streamlit that I hadn’t paid attention to in the last year or so. This book is great for someone seeking a quick recap of all Streamlit features. I managed to read it cover to cover in a few hours. It is quite a surprise that this company was acquired for 800 million USD. Compared to Streamlit, Shiny, the web application framework in the R world, is far more powerful and flexible. Streamlit, however, has filled an important gap for data scientists primarily using Python, and perhaps the timing of its venture was perfect.

My go-to tool for prototyping or showcasing has always been Shiny. Since the introduction of modules, I have grown even fonder of Shiny, as they have made developing large apps hassle-free. Although large apps can technically be created in Streamlit, I don’t envision creating apps with more than a few pages. In contrast, I have developed several Shiny apps with over 50 sub-pages, and modules make this inherently easier in R.

In any case, this book served well as a quick refresher. Creating a hello world type of app in Streamlit is reasonably quick. However, for creating a multi-page app, one must delve into the documentation. The Streamlit documentation is excellent, unlike that of most open-source packages, but it might still be daunting for a newbie. This book is useful in that sense. If you code along with the various chapters, you will gain a good working knowledge of all aspects of web app development using Streamlit.

The first chapter offers a quick recap of all the web app frameworks that preceded Streamlit. It includes instructions for installing the library and running bundled examples. For someone who isn’t familiar with the way Streamlit works, the chapter can serve as a primer to Delta Generator, the middleman between Python script and the ReactJS application. Every library that pitches itself as no HTML, no JavaScript needed to data scientists and data engineers, the heavy lifting of translating R snippets or Python snippets involves a ton of HTML and JavaScript code. In the case of Streamlit, the front-end JavaScript framework used is ReactJS. One thing that tricks most developers is the fact that Streamlit runs the entire script whenever there is change of state in any of the widgets on the app. This is unlike most of the other frameworks out there such as Shiny. Why the framework re-runs everything for a widget state change is explained in a few lines from the book on the architecture, which might prompt further exploration by the reader.

The DG takes care of efficiently transferring HTML components to be rendered on the client side and retrieving back their state. The initial render starts from the beginning of the Python document and ends at the last line. The next render does not start from the beginning of the file executed, but from the component which had been interacted with by the user or changed state;Every new render of a component will be queued in the DG, which will later be replaced with a new HTML snippet or be newly placed between other rendered HTML components on the final DOM. Streamlit components are queued and rendered individually to avoid negatively affecting the user’s experience with a blank page if renders take too long.

Streamlit uses built-in ReactJS components grouped to make a fully fledged JavaScript web application with Python! In addition to that, we can leverage Streamlit’s generic treatment of components to build custom and complex ones that are not provided out of the box

The second chapter of the book gives a quick primer to all the main elements that one would need to build a toy webapp. Some of the functions covered in the chapter are

  • st.form
  • st.columns
  • st.slider
  • st.text_input
  • st.date_input
  • st.radio
  • st.form_submit_button
  • st.info
  • st.stop
  • st.error
  • functions to rendering dataframes
    • By placing a pandas.DataFrame object on a line in the script, the framework automatically renders the object on to the app. Depending on whom you speak to, this feature might be considered as super cool or super weird
  • functions to render plots

The third chapter focuses on explaining the functions to organize the web page in to sidebars, columns, expanders, placeholders. Most of the functionality provided by these functions are obvious. However placeholders and containers are some of the really cool features of the framework that will be massively appealing to any Python dev as it gives enormous flexibility in structuring a web app. The chapter talks about building multi-pages using a hack, i.e. using a select box and then opening a page based on the choice in the side bar. This was probably the way to go about before Streamlit introduced the multi-page feature in 2022. I think the authors had finalized the content of the book before this feature came in and hence this part of content is sort of obsolete. Recently I have also come cross st_pages plug-in which makes it even more convenient to create a multi-page app.

The fourth chapter goes in to details about providing data munging and visualization features that are embedded in to Streamlit. The fifth chapter goes in to helping the reader connect one’s app to various SQL and NOSQL databases. Nothing can beat the detailed documentation on the Streamlit site that gives details about connecting to

  • S3
  • Big Query
  • Firestore
  • Google Cloud storage
  • MS SQL Server
  • MongoDB
  • MySQL
  • PostgreSQL
  • Google sheets
  • Snowflake
  • Supabase
  • Tableau
  • TiDB
  • TigerGraph

Feels nice to see TigerGraph. It was 6 years ago when I was actively in touch with the TigerGraph team to build some POCs. Haven’t interacted with the team since then, but looks the company is doing really well. In any case, the point of the chapter is to make one realize that there are third party packages that connect Streamlit to whatever db that you might be storing data.

The sixth chapter is about API development, the idea being one should create a separate layer that Streamlit can access instead of being a code heavy layer. Having a detailed chapter on developing flask application is really not necessary as that itself can be a book length treatment. May be this is the issue that plagues any book that is written by multiple authors. It is difficult to cut down content if multiple authors are involved in writing a book.

The seventh chapter is about session management. Despite the chapter length treatment in the book, I have really found a short tutorial on Streamlit site to be much more concise and to the point. Frankly I don’t think any data scientist or someone who wants to develop quick prototypes will find this chapter useful.

The eight chapter is about authentication and application security. Again this chapter is stretched beyond what is needed. Given that Streamlit is mainly targeted towards data scientists, a basic authentication like using secrets.toml should suffice if you are hosting on Streamlit community cloud. A short article on this topic on Streamlit blog is more than enough for most of the apps.

The ninth chapter talks about making Streamlit app accessible to everyone. This chapter is of course very important as the whole purpose of Streamlit framework is to share data science work with others. I was quite familiar with the options and hence did a speed read of this chapter. Deploying to Streamlit community cloud is the easiest of options. The other option of course is to put everything in a docker container, make an image of it, and use any of the cloud providers infrastructure to host spin up services based on the docker container. I have found that AWS Fargate offers a very convenient way to host Streamlit apps. There is a one time learning curve in understanding AWS Fargate and subsequently you will have a powerful way to distribute your apps in a secure way. Also for security reasons, you might want to spin up a load balancer with in AWS infra so that you can install specific security certificates to enable HTTPS. The steps that one can follow to host a Streamlit app securely on AWS are

  1. Spin up a VM or use your local machine to create a docker image
  2. Register the image in AWS Elastic Container Registry
  3. Create a AWS ECS Cluster
  4. Create a Task where you define the VMs, the infra and the relevant docker image that you want ECS cluster to run. Run the relevant task via your ECS cluster definition
  5. Create a AWS Load balancer and use it to front any requests to the Streamlit app running on the ECS cluster
  6. Enable HTTPS and install the relevant certificate

The above half a dozen steps should take about 15 to 20 minutes for someone familiar with AWS components. If someone is doing for the first, there is a learning curve for each of the six steps. However given the wide spread AWS documentation , blog posts, videos on AWS components, it is very easy to learn and get this done. Having said that, if you are looking a light weight app sharing mechanism, nothing beats Streamlit Community cloud option

One of the penultimate chapter relates to building Streamlit components. The book ends with a few chapters on use cases. I found the use cases pretty trivial and not containing much of a substance. However it might be useful for others.

Takeaway

I found this book to be a valuable quick refresher on various Streamlit concepts. While it doesn’t have standout features, pairing each chapter with corresponding official Streamlit documentation and reading the pairs simultaneously seems to be an effective learning approach. The book deviates slightly from Streamlit’s ethos of simplicity and agility in web app development, as it delves too deeply into general web development aspects. For future editions, it would be beneficial if the authors streamlined the content to align more closely with Streamlit’s philosophy of being a quick and lightweight framework for agile web app development. May be cut the book down to half so that it makes it easier for the reader to grasp the essentials.