How to Turn Python Code into a Streamlit App

Building Python Scripts to speed up your tasks or automate tasks is fun & there are IDEs like Replit that kind of act like an App however they aren’t exactly apps.

What if I told you there is a way to transform your Python Code into a functional App that has a proper User Interface which will visualize the output in an intuitive way?

And the best part is that it is free!

Step 1: Transform your Python Code with Streamlit Import & some UI Aspects

Some of the code elements that the transformed Python Code would need are

				
					import streamlit as st
				
			

This line of code imports streamlit which is where we want to create the app.

				
					st.title('Google Search Console (GSC) Queries Race Chart')
				
			

st.title is the syntax that you use to display the title of your app.

				
					st.plotly_chart(fig, use_container_width=True)
				
			

Since I was creating an app that visualizes Plotly data that is why you would see st.plotly.

In a nutshell, this is the kind of code modification that you will need to make.

Pro tip: Here is the ChatGPT prompt that can transform your existing Python Code with Streamlit elements

				
					I have this python script that creates race chart visualization on GSC queries

here is the code
(paste the code here)

Transform this code in a way that I can turn this into Streamlit app; so the transformed code I will put on Github & connect streamlit via Github link & then I would like to see the visualization on Streamlit with play, pause button on Streamlit i should have option to upload csv file after which visualization should result


				
			

Step 2: Create your requirements.txt File

				
					streamlit>=1.7.1
pandas>=1.4.0
plotly>=5.5.0
				
			

Here is an example of my requirements.txt file. This is what Streamlit is going to read to install the requirements.

Step 3: Move the Modified Python Code & Requirements.txt in GitHub

python code on github

Here in the above screenshot you can see how I have moved my Python Code & requirements.txt file into GitHub Repository.

You will have to create a GitHub Repository first & then move these two files there by manually creating it.

For the Python Code file it should end with .py that’s the file extension and the requirements file should end with .txt 

Step 4: Go to Streamlit.io > Create App > GitHub Option

streamlit step 1

As a first step click on “New App

streamlit app step 2

Step 5: Paste GitHub .py File URL, Name the App & Hit Deploy

Deploy-an-app-·-Streamlit

Once you deploy your app you will be able to see it as a functional app. Sharing an example of my app below.

Here is an example of an app that I built whose backend GitHub code is Python. You have to upload a csv file containing following columns. In fact you can use my app here itself in this page as I have embedded this.

  • query
  • date
  • clicks

Based on that you will get Race Chart Visualization Here based on dates. You will be able to see Query trends across the date range.

Leave a Comment