Flask’s Latest Rival in Data Science
By: Mohammad Ahmad
Tags: Programming, Artificial Intelligence, Data Science, UX, Machine Learning

On the comparison between Flask and Streamlit: a reader noted that Flask has capabilities in excess of Streamlit. I appreciate this point and would encourage users to look at their use cases and use the right technology. For the users who require a tool to deploy models for your team or clients, Streamlit is very efficient, however, for users who require more advanced solutions, Flask is probably better. Competitors of Streamlit would include Bokeh and Dash.

Streamlit

This is where Streamlit comes into its own, and why they just raised $6m to get the job done. They created a library off the back of an existing python framework that allows users to deploy functional code. Kind of similar to how Tensorflow works: Streamlit adds on a new feature in its UI, corresponding to a new function being called in the Python Script.

For example the following 6 lines of code. I append a “title” method, a “write” method, a “select” method and a “write” method (from Streamlit):

import streamlit as st st.title(‘Hello World’)

st.write(‘Pick an option’) keys = [‘Normal’,’Uniform’]

dist_key = st.selectbox(‘Which Distribution do you want?’,keys) st.write(‘You have chosen {}’.format(dist_key))

Save that into a file called “test.py”, then run “streamlit run test.py” and it produces the following in your browser on http://localhost:8501 /:

Code above produced this. Fantastic how efficient Streamlits library makes UI programming.

Now this is awesome. It’s both clean to look at and clearly efficient to create.

Jupyter Notebooks is also another successful “alternative” but it’s a bit different. Notebooks is better as a framework for research or report writing however there’s little you can do in the way of actually letting someone else use your code as it’s impractical to give someone else a notebook of code. Co-labs kind of bridges that gap but it’s still not as clean.

Streamlit fills this void by giving the user an ability to deploy code in an easy manner so the client use the product. For those of us who like making small things, this has always been an issue.

Ease of Use

Ok so let’s create something that we may actually want someone else to use.

Let’s say I want to teach my nephew about distributions. I want to make an app that he can use where he selects a distribution, and then it draws a line chart of it. Something as simple as the following:

Code provided below how to create this

In this example, you can see that the user has a choice between 2 items in a drop down menu: and when he selects either, you hope that the line chart would up date with the chart. Taking a step back, I’m providing the user with:

Some Information about a problem The user then has the ability to make a choice The corresponding chart is then returned to the user

Now in Flask, something like the above would easily require hundreds of lines of code (before even getting to the aesthetics) however Streamlit have achieved the above in a negligible amount of code. Note that the above required the following ~11 lines of code:

import streamlit as st

import numpy as np # Write a title and a bit of a blurb

st.title(‘Distribution Tester’)

st.write(‘Pick a distribution from the list and we shall draw the a line chart from a random sample from the distribution’) # Make some choices for a user to select

keys = [‘Normal’,’Uniform’]

dist_key = st.selectbox(‘Which Distribution do you want to plot?’,keys) # Logic of our program

if dist_key == ‘Normal’:

nums = np.random.randn(1000)

elif dist_key == ‘Uniform’:

nums = np.array([np.random.randint(100) for i in range(1000)]) # Display User

st.line_chart(nums)

I find it amazing because the amount of code required is so small to produce something that actually looks and works pretty good.

For anyone who’s played around with UI before, you’ll know how difficult it is to achieve something of this quality. To have Streamlit produce an open-source framework for researchers and teams a like, development time has been immensely reduced. I cannot emphasis this point enough.

Given this, no Data Scientist or Machine Learning Researcher can ever complain about not being able to deploy work. Nor can they complain about getting an MVP running. Streamlit have done all the hard work.

Amazing job guys!