Introduction : What is Streamlit
On Streamlit website, Streamlit is proclaimed to be
“the fastest way to build and share web data apps”.
While I am unsure whether Streamlit is the fastest way, I reckon Streamlit makes creating web-based applications with python quite easy. Streamlit is a python package that helps developers quickly build a simple web application on server. The web applications must be simple. The framework excludes many standard functionalities such as authentication, encryption, unit-testing and other essentials for production. Its documentation and examples seem to suggest that the web applications are not targeted for scale (ie several servers handling requests from millions of clients). It is a python package that is compatible with many existing libraries, most notably Matplotlib and Altair for visualization, Numpy and Pandas for number crunching, Pytorch/ Tensorflow and other frameworks for Deep Learning. The development on Streamlit is quick because of two handy features. As it relies on a python interpreter, it runs as the interpreter reads the source code sequentially. Problematic lines or bugs can be found as soon as the execution breaks. In addition to the ease of debugging, Streamlit monitors changes in the python source code, re-runs the web server, and reflects the updates of those pages automatically. So developers can see the changes and improve the code as they see fit in real-time.
Nowadays, packages seldom remain packages. Streamlit has built its own community of developers and users. While there are plenty of helpful posts on forums concerning many issues and desirable features to be added, there is much room for improvement. For instance, as of this time of writing of this article, to my knowledge, I find it frustrating that Streamlit has yet to provide an api for interactive images. What I meant by interactive images is something along the line of clicking on an image as though the image is a button. Upon a click, we trigger an event that either records new data point or submits a request to server.
I reckon web applications are becoming increasingly popular. As remote work is gaining traction in the industry, we may find ourselves working with others across the country or the world. It is hard to share and work together if the application is local. A viable alternative is to move the application (output of the development) to the web server whereby team members can view and interact online (ssh to server, sftp for file transfer). Visualization and Interaction are very crucial for work in mathematical modeling, including but not limited to data sciences. I would like to emphasize this point by supplying some screen-shots on the capability as of August 2022.
Screen-shots
- Let’s see how the traffic varies throughout the day at different traffic hubs.
- An intuitive and visual way to explore model’s performances (objection-detection models).
- Let’s try the model on someone else data.
- Wow! we can even generate and play audio clips.
- Wow! Wouldn’t VR be useful for collecting feedback on our creation from other team members?
I cannot help but to borrow a famous quote from Ralph Waldo Emerson.
Numbers serve to discipline rhetoric. Without them it is too easy to follow flights of fancy, to ignore the world as it is and to remold it nearer the heart’s desire.
Prerequisite
- Install Python 3
- Install Streamlit and its dependencies
Why do I use it?
In the past, I have tried to create an executable for my application which is then shared in our team as a proof of concept. The python package I used is Pyinstaller. Pyinstaller creates an executable by bundling all the dependencies (libraries installed in an existing virtual environment). The executable must be created in the same operating system as the one it will be used. For instance, if the executable is created in Windows, other Windows computer can run it without a python interpreter and dependencies or libraries.
When the executable is being created, Pyinstaller bundles all the libraries that may be irrelevant together. Thus, the final executable can be very large even though the application is quite straightforward and simple. While Pyinstaller is compatible with many commonly used python packages, and I have yet to run into incompatibility issues, I fear as the python community develops, it will be challenging to accommodate more complex needs of developers.
My remarks and comments
When I first try Streamlit, I feel like a latex user discovers the power and convenience of Microsoft Word. In the old days, I used to rely on Latex to format and design the layout of my thesis. While Latex is very good at specifying dimension, style, font, and whitespace at the level of paragraphs, pages and document, it does not display the final look of the document unless the user can successfully compile the code. Compilation can bring about its own troubles and difficulties. Even upon a successful compilation, the final look of the document often differs from the expectation of the user. What happens next is debugging, rewriting and recompiling, leading to much frustration. Latex is very flexible and versatile, so eventually I got the look I desired.
What Microsoft Word solves and I reckon Streamlit is doing the same is that Word displays the effect of the command immediately and takes all the guess work out of the process. I like fast feedback loops and intuitive ways of building things. Streamlit helps me take care of HTML, JavaScript, and web server framework. It creates and runs a simple web application, sufficient for prototyping and proof of concept that we will need to succeed in a team. I highly recommend Streamlit for those who work in a team environment and need to constantly share application prototypes and ideas consistently. It helps developers focus on what is the most important: insights in data.