In the previous lesson we learned how to build a machine learning web application using Flask, writing basic HTML, and deploying Flask apps with Heroku. But, what if we could build those apps as easy as writing Python scripts?
Imagine you can convert simple python scripts to beautiful web apps. Well, that tool exist and is called Streamlit.
Streamlit is an open-source framework to create data science and machine learning apps for data exploration in the fastest way possible. It even gives you a real-time coding experience. You can start your streamlit app and every time you save, you see your code reflecting in the browser at the same time!
Now, let's go ahead and install Streamlit using pip:
Once the installation is complete, use the following command to see a demonstration of an application with example code:
You can now view your app in your browser: http://localhost:8501
The simplicity of these ideas does not prevent you from creating incredibly rich and useful apps with Streamlit.
Streamlit apps are pure Python files. So you can use your favorite editor and debugger with Streamlit.
Pure Python scripts work seamlessly with Git and other source control software, including commits, pull requests, issues, and comments. Because Streamlit’s underlying language is pure Python, you get all the benefits.
Streamlit provides an immediate-mode live coding environment. Just click Always rerun when Streamlit detects a source file change.
Streamlit is built for GPUs. Streamlit allows direct access to machine-level primitives like TensorFlow and PyTorch and complements these libraries.
Here we will explain some of the basic features but for a complete Streamlit documentation you can clic on the following link:
There are many widgets available, including the following:
The first parameter is the title of the selectbox and the second parameter defines a list of values to be populated in the selectbox. Here, the second parameter is a column name ‘State’ in the .csv file we loaded.
The first parameter in the checkbox defines the title of the checkbox, the second parameter defines True or False whether it is checked by default or not and the third parameter defines the unique key for the checkbox.
The problem with many dashboarding tools is that data is reloaded every time you select an option or switch between pages. Fortunately, Streamlit has an amazing option allowing you to cache the data and only run it if it has not been run before. You can cache any function that you create. This may include loading data, but also preprocessing data or training a complex model once.
Streamlit supports many visualization libraries including: Matplotlib, Altair, Vega-Lite, Plotly, Bokeh, Deck.GL, and Graphviz. It even can load audio and video!
A different example:
To plot the graph we used plotly.express library’s bar method. The first parameter is the dataframe we want to plot, the second parameter is the column for the x-axis, the third parameter is the column for the y-axis, labels parameter is optional in case you want to change the name of a column for the graph and the color parameter here is to color code the graph on the basis of the Status column from the data frame.
We can generate markdown and beautiful READMEs with only a single function:
The Write function behaves differently based on its input. For example, if you throw in a Matplotlib figure it will automatically show you that visualization.
A few examples:
write(string) : Prints the formatted Markdown string. write(data_frame) : Displays the DataFrame as a table. write(dict) : Displays dictionary in an interactive widget. write(keras) : Displays a Keras model. write(plotly_fig) : Displays a Plotly figure.
Let’s see how can we create a very basic example web app. We will first create a python file app.py and import the libraries we will be needing.
We then import the data:
In the load_data() method, we are reading the .csv file using pandas library and we are making our code efficient by caching the data. If this data would keep changing, we are clearing our cache every 5 mins or for a maximum of 20 entries. If the data does not change very often, we can simply use @st.cache(persist=True). The code above is an example, but for Titanic model, we could keep the persist=True.
Now let's create a title, some content and a side menu
The description shows in blue color because we used HTML to give the custom color as blue. We can also use header and subheader like we used st.title() for different headings. Or we can use markdown for that purpose.
Anything we call with sidebar will show in the sidebar.
Once you are done creating your own app, you can run it using:
Now that we have a very basic web app, we can show it to others by deploying it to Heroku. Of course Heroku is not the only free option in the market. A different free option could be Amazon EC2, and many others.
If you had already installed the Heroku Command Line Interface (CLI) from the previous lesson, then you are ready to go. If not, you can do it from here: https://devcenter.heroku.com/articles/getting-started-with-python#set-up
This will help you manage your application, run it locally, view its logs, and much more.
Open your cmd.exe and enter to the application folder.
Log in to Heroku with
heroku login. You will be redirected to a login screen on your preferred browser.
While having your cmd open in your application folder, first run
heroku create to create a Heroku instance.
Push all your code to that instance with
git push heroku master.
This will create a Heroku instance and push all code in your application folder to that instance. Now, the app should be deployed.
heroku ps:scale web=1 you will make sure that at least one instance of the app is running.
heroku open to open your application on the browser.