Deploy and Host your Data Science Project | by Merlin Schäfer | Towards Data Science

You’ve analyzed a data set and found interesting insights, you have built a machine learning pipeline, but so far they just live within your jupyter notebook. If others want to view your work they have to read through your notebook and view every output, that’s only ideal in a few cases. It’s time to take your work and showcase it interactively.
This does not have to be hard and you don’t require the help of a front-end developer. You, as a data scientist or someone on the path to becoming one, can deploy and host your project or application.
All you need is streamlit.
Streamlit allows you to create apps with a lot of functionality using just a few lines of code. You can host these apps locally or deploy them online. In the past, you had to use Heroku or a similar service to deploy your apps online, but now streamlit has its own sharing platform which makes deployment very easy.
I will start with a quick overview of some basic streamlit functions so you can begin building your application and then go over the process of hosting an app on streamlit sharing.
If you already know how to use streamlit feel free to skip to part two, where I quickly explain streamlit sharing.
For this tutorial, I chose the California Housing dataset. You can find it here.You can transfer anything I show here to your own projects and that’s really the goal. Let’s get started.
The first thing to do is to install streamlit this can be done via pip or conda.
pip install streamlitconda install streamlit
To check if your installation worked you can type
streamlit hello
After installing everything you can get started with your app. The first thing you need to do is import streamlit (usually as “st”) and all the other libraries you require.Now you can read in any data you need and modify it if necessary. Here I read in the housing data and add another column which I will use later in the machine learning part.
Now you can focus on the streamlit part. Your app should have a title, to give it one you just need to type st.title("Your chosen title") .
If you like to display an image you can use the st.image() function which accepts a wide range of inputs such as image-URLs or numpy arrays and displays your image. You can manually adjust the image size (e.g. width = 700).
To give your users some context you probably want to display some introductory text or instruction on how to use the app. Streamlit has many functions for that, I opted for the st.write() function which is a “swiss-army-knife” that can display text in a markdown format (# for titles, * to highlight, etc.) but also accepts dataframes and other objects.
The results of the code above are this:
Now let’s get to the functions that let you display your work!If you want to display a dataframe you can usest.dataframe(), which will dynamically display the dataframe, allowing you to scroll through it.
Giving the users some option to filter the dataframe and only look at specific columns is not very hard and can be implemented via st.mulitselect().This function takes in 3 arguments: the text to display above the selector, the selectable fields as a list and the default fields to display. You can use the funtions ouput as a list to subset your dataframe and thus only display selected columns.
A dataframe is nice, but often a good data visualization is a better choice if you want to present your insights. Streamlit makes it very easy to display them.
All you need to do is create a figure via matplotlib and/or seaborn and then pass that figure to st.pyplot(). You need to explicitly create a figure with axes and can’t just call a seaborn or pyplot function without. This only works in jupyter notebooks with some jupyter magic.
If you want to showcase any geographical data I recommend using st.map() as it automatically creates an interactive map with your data points, as long as the data contains longitude and latitude.
In the following part, I will quickly showcase an example machine learning app that takes in some user input and outputs a prediction. The model used here is not good and only serves as a dummy for the tutorial.
After you’ve trained a model that you want to deploy, save it in a pickle file and load it into your streamlit file. You can use the loaded model like any sklearn model object and simply call .predict() on a given set of inputs. Just remember to pass all of the inputs the model was trained on otherwise you will get an error.To keep it simple I chose only four numeric inputs for the model.
The user input can be placed in a sidebar, where all of the features can be set. To place something in a sidebar you need to add st.sidebar before your function. In this example, I use a .slider() for every input feature. This function takes a description as a string, the start, end, and default value for the display of the slider.
The slider is a good choice if you have numeric continuous variables, if you have categories or binary inputs a selectbox or other functions are good choices.
The slider values are all placed inside a function and collected in a DataFrame which the function returns.
This dataframe is used by the model to get a prediction. You can use st.write() and some minimal formatting to display your prediction in a simple but clear manner.
To host your application locally open your terminal or command/anaconda prompt navigate to the directory your Python file is located in and type streamlit run {your_file_name}.py. A browser window with your app should open automatically, if this doesn’t happen, use the localhost provided(normally it’s localhost:8501)directly.
You can find more info about how to use streamlit in the excellent docs.
Not long ago Streamlit created their own hosting service called streamlit sharing. It’s still in a testing phase but you can (and should) request an invite to you it.
Request an invite link to streamlit sharing here.
Once you have your invite link you need to sign up with the email address you use for GitHub. This is necessary because streamlit sharing needs to access your public repositories to host your app. After the login you should see this:
Before you can host anything, streamlit python script, all necessary files (e.g. model files, csv-files) need to be in a public repository. You also need a requirements.txt file that lists all packages(including their versions) your app uses. If you are unsure how to create one check out the streamlit docs.
Now you only have to enter your repository name, branch, and the name of your streamlit python script.
After you’ve hit deploy you should see some animations. You can check the progress (or any Error Messages) in the bottom right corner under “Manage app”. Once your app is deployed it should work like the local variant, but now you can share it with whomever you want!
I hope this helped you to get started with deploying and hosting your Data Science and Machine Learning Projects. It’s time to show everyone what you’ve created!