Deploy a Streamlit application on Google Cloud Run - all you need to know

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone my name is federico tartarini and in this video i'm going to show you how to deploy this simple streamlet application using google cloud run so you're going to expose your application online at this public url of course url is going to be different and everyone with this url will be able to access your application so this is a great way if you have created like a streamlit application locally or if you have some python code and you want to showcase your work to deploy your application in a very simple way it's going to be most likely free because google cloud run offer quite a good free tier that you can use it for free and just in case you get billed because your application is used a lot google cloud run it offers like a positive advantage compared to app engine or any other solution such as the roku that you're going to be build only when your application is used because it's a serverless so basically you don't have to maintain the server and you don't have to worry about updating a server and downtime because all that is going to be managed by google if you want to find out more about cloud run i would suggest you to look at their official website in which they explain all the positive features and i'm going to suggest you to look at my previous video in which i show how to deploy a flask application on google cloud run and explain a bit what google cloud run is and which are the benefit compared with other services such as your oracle or even app engine streamlit on the other end is a very nice open source python package that allows you to build powerful application with very little code so basically you don't have to add much to the code that you've already written in python but you're going to be able to create a very nice application such as this one that i was showing you before and you don't need to have any front-end skill development so you don't need to know how to write javascript or react you just convert your python code in this beautiful web application in this tutorial i'm going to use a boilerplate from streamlet so here on their website they show you how to create a data explorer app so this is what i'm using specifically for this example i'm using this code here at the bottom of course feel free to use a different source code or change these based on your requirements and to deploy any streamlet application okay so as you can see here i've deployed online and i've deployed locally so let's go step by step and i will show you all the things that you need to do to deploy it on google cloud run so to follow along with me in this tutorial you will also need to install google cloud sdk command line tools it's not really required as i've showed you in my previous video on flask which i'm going to put a link here at the top you can also do that by pushing your code to github and then cloning it online and deploy your application but i think it's much easier if you install google cloud sdk command line tools so you can just go here on google search for google cloud sdk installing and i will put a link down in the description and then here it explains how to install it it's pretty simple and pretty straightforward you just have to run this command based on your operative system okay so now that we know what we need to do to get started let's create a new project with pytharm i'm going to use pytharma for this tutorial just because i think it's a great ide of course feel free to use any other id or in fact you don't even need an ide because it's going to be very simple to deploy this application but i think pytharm offers like a lot of nice features for free so i'm going to use that so we're going to create a new project so we're going to create this project in my case i'm going to create it on the desktop and i call it python project i'm going to create a virtual environment it's always a good practice to create a virtual environment when you create a web application because so you're going to be able to manage all the dependencies and test that everything is working locally before you push it to the cloud so we are going to here by charm we are going to create a new python project using virtual environment python 3.8 is fine we are not going to inherit the global site packages and we are not going to make it available to our project so we can click on create and we are creating a project so paicharmas created the project for us which is located in my desktop gita project python project and has already created a virtual environment for us we can check that by going here at the bottom into the terminal and we can type pip list and then we're going to see that we only have pip and setup tool installer because we created just a new virtual environment of course if you're not using pycharm you can still create a virtual environment and then you have to activate it i will just put a link down in the video description on how to do that or just google how to create a virtual environment in your project okay so the first thing that we need to do of course is to create a python file which are going to call it app and this will contain all our streamlead code and let's go and copy directly from their website so we can go back here we can go to their website and we are going to copy all their code again feel free to change this code and to edit based on your needs so we can just copy this code here and this is the code that we have now we can see that pycharm is highlighting that this is here red because we don't have streamlit pandas or numpy installed so let's go and install them so we're going to pip install and then we can say pip install streamlit of course in pycharm you could have used alt enter and then it's the install package streamlit but i just want to show you how to do it from the terminal so we do pip install streamlit and this is going to install streamlit and automatically also is going to install pandas because it's a dependency and numpy while this is happening we can go again here a new python project create a new file inside the same directory and we're going to call it requirements dot text in this file we are going to write all the requirements that we need so in this case i'm using this version of the three python pandas numpy and streamline of course you can use a more updated version if you want to i would recommend you to just specify a version so you always can check that everything is working there and then of course when a new version of this package is going to be released then you can update that as you can see right now you're seeing that it's not installed because you're just installing it so we just have to wait for a second and these are the only three dependencies that we have in our project it says it's not satisfied just because it's installing so just let's wait uh for a minute and that's going to be fine that my error message is going to go away then i'm going to create a readme file in which i'm going to save the command that we're going to use a readme dot markdown file in which i'm going to save the command that we're going to use to deploy our application of course i forgot to mention it before but i'm going to make this code available on github so check down in the video description and as always you will be able to find a link with the source code so you can just copy and paste and deploy it on your application i'm going to copy and paste some boilerplate code here and i'm going to explain exactly what i'm doing here and which command are this one just keep in mind that you will have to change something in your specific um case because of course you are going to deploy it in a different project so great now we have installed the requirements and as you can see here we have all the requirements that have been installed now we want to launch our application locally so we can do with streamlit and then we can say run app.pi and app.pi is the name that we gave to the python file so of course if you call it in a different way you have to change this name so let me run this one so it's running on localhost so if i click here it should open the application create and it shows uber pickups in new york loading data is running the load data and it's going to display in the data in a second so here we are running the application locally after we check that everything is working we want to then deploy to google cloud run okay so the application is running let me go back here and you say uber pickup in new york city i just want to show you that we can update the application it should have auto reload so if i save this file here and i go back to my browser and i refresh this page we can see uber pickups in new york city i just wanted to show you i mean i don't want to change anything else because of course then you can change all the chart and everything but the scope of this video was just to show you that it's not static you can also change the code and then it's going to be updated automatically so great now we have done almost everything that we need to do we just need to go back to our project so here in um we just have to go here in pytharm so we have the app.pi file we have the readme which i'm going to explain in a second what i put here inside this readme and just you know in a second i will explain everything i have the requirement.x file which shows us which are our dependencies we need to create one last file which is called is a docker file okay so we create a docker file and in this docker file we are going to copy some boilerplate text as i previously mentioned don't worry too much because i'm going to put these sorry i haven't copied the full text so i'm going to put this code in a github repository which is going to be public so you're going to be able to copy this code without any issue but let me explain more or less what i've written here in the docker file so we're just specifying the python so we specify that we need python 0.3 and then we have to expose a port we are exposing a port for streamlit to show our application and we are going to reference this port here down so we are exposing this port and we are actually going to deploy our stimulate application on port 8080 then we are copying all the file inside the docker so all the files that are inside this project we are copying it inside docker we are installing the requirements with docker with this command run pip install our requirement.txt and then we are running the application so we are doing it almost exactly as we are doing it locally because as you can see locally we say streamlit run up.pi but the only thing that we need to add if you want to deploy it on google cloud run is the server enable course false and we want to change the port just because we want to expose it on port 8080. so these are the two main things that you need to change and you don't actually have to change you just have to write it here in the docker file but is the major difference from running it locally and running it inside the docker file so now it's perfect we have the app.pi file with all our slimlit application we have the docker file with all the commands needed for docker to build our application then we have the readme which contains the google cloud console commands which i'm going to tell you in a second and we have the requirement.text of course if you want to be this is i want to keep it simple you can also add a docker ignore file and you can also add a gcloud ignore file i will put a link here at the top in the video description to another video in which i explain those two files but i wanted to keep this video very simple because it allows everyone even with a limited knowledge with how google cloud run works to get started so let's go to the next last step and how to deploy this application on google cloud run so in order to deploy the application google cloud run we're going to use this command so first we're going to build and submit our application so we're going to build it and submit it to google cloud run and then we're going to deploy okay so most of this code you will not have to change it aside of these two part here so the name of the project and the name of the application as you can see is repeated here and here so you will need to change that based on your specific google cloud run configuration so how do you find this information well let's go back to the browser and we can just go in google cloud run you can click here and you go to the console i've already opened it so i'm already inside the console of my project this is what is going to be prompted if you click here on console so here we are we have to go here at the top and you can see here there are all my projects if you don't see any project or if you have never created any project before you can just click here new project and create a simple project and you can call it streamlit application in my case i already have a project created so i'm not going to do that i'm going to deploy it here on testpad so you're on testbed you can see the name of the project but we are what actually we need to put in that command is the project id so is this string here so i'm going to copy and then i go back here and i paste it here and as you can see i've already done it so i don't need to change it so this is the project id and has to be copied here and here and this is the application name what is the application name well inside google cloud run this is the name of our application that we're going to see here so at the moment i already have extremely tutorial application but i want to deploy another one which i want to call streamer tutorial youtube it's going to be exactly the same just with that mine minor edit where we say new york city but i want i need to give a different name if i want to deploy it here so let's go and deploy that so the first command that we need to use is this one we just have to copy and paste it here in the terminal so let me go let me actually just close this terminal i'm going to terminate the local application so now locally is not going to work anymore and then i just copy and paste this command and then i press enter this command is going to take a couple of minutes and it's just going to build our application and send it to google cloud run so i'm going to fast forward this part but you can see all the output still in the video so great everything was fine we can see a success message here so it tells that everything worked fine now we need to deploy the application so let me go back here to the cloud console and you can see that there is only one application let me refresh these because we haven't deployed yet so here we have just one application let's go back here and let's run this command it's going to ask us a couple of questions which i'm going to show you what you need to answer so basically the first one is going to be the service name just because we didn't specify here in the tag so we are happy to call it slimly tutorial on youtube but you can change that of course if you want to but i'm going to press enter because i define the name here same tutorial youtube and then you have to specify where you want to deploy your application you can deploy it wherever you want or i can deploy it in asia asia south so basically you should deploy it closer to where most of your users are going to be so i'm actually in singapore so i'm going to deploy it in asia's out which is going to be number six and i just have to press enter this process also is going to take a couple of minutes and if everything is fine and if everything succeeds then we're going to get a url that we can click on and we can open our application online and then we can share our url with everyone else [Music] great everything goes fine as you can see says done success everything is fine we can refresh the console here online just to check that our application has been successfully deployed and we should see the new application extremely tutorial youtube fantastic is there and is asia's out it is very important that here in this command you allow an authenticated call what does it mean is it means that everyone without logging in can access your application and that's the key because you want that everyone that visit this url is able to access your application so let's now click on the url we're going to be redirected to the url and we should see our website i just want to mention that sometimes it can happen that you get an error when you are loading the application for the first time it didn't happen this time but if it happens just refresh the application and great now we have our application deployed online and we can just play with it and we can share the link of the application with the friends for links or we can share it online to showcase our work i really hope you find this video interesting if you have any comments or questions please write just a comment down in the video description and if you found this content the content of this video useful please consider liking this video or subscribing to my channel it really helps me a lot because i'm new on youtube so my channel is still growing so it really helps me if you could just consider subscribing or liking my video and if you want to support my channel you can buy me a coffee and you can find the link down in the video description thank you very much for listening and see you next time
Info
Channel: Federico Tartarini
Views: 15,580
Rating: undefined out of 5
Keywords: deploy a streamlit app on Google Cloud Run, Streamlit app Google Cloud, deploy a streamlit app, streamlit python tutorial, deploy streamlit for free, python streamlit, streamlit tutorial, machine learning, streamlit python, deploy streamlit app, how to deploy streamlit app, deploy streamlit app on google cloud, Python streamlit tutorial, streamlit apps, data science, streamlit google cloud run, streamlit google cloud, deploy streamlit
Id: LxwoCKM1Qik
Channel Id: undefined
Length: 19min 19sec (1159 seconds)
Published: Sat Jun 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.