Streamlit Explained: Python Tutorial for Data Scientists

32.78k views2979 WordsCopy TextShare
ArjanCodes
In this Python tutorial, I’ll dive into Streamlit, a tool that simplifies the creation of web applic...
Video Transcript:
today I'm diving into streamlit a tool that simplifies creating web applications for your data science projects we've been using streamlet ourselves for building a data dashboard that shows us useful stats about Maran codes it's really easy to get started with it and this is what we're going to end up with at the end of this video amaz generator some of you also asked about the difference between streamlet and other librar such as Dash or typi I have some thoughts about that at the end of the video regardless of what library you choose you'll still need to think about the rest of your dashboard architecture how you collect and transform the data where to store it security precautions and so on if you want to learn how to make architectural decisions like this I created a free architecture checklist go to iron code checklist to grab your copy contains a ton of practical advice and a list of important things to think about that you can take off so you don't forget anything earn. code checklist the link is also in the description of this video the first thing we're going to do is get streamlit up and running now you can install streamlit using pip but if using poetry you can simply write poetry at streamlet and then that's going to add it to your P project file now I already have a p project file in this particular example this is where it is and you see we have streamlet and we have a couple of other libraries as well this is all in the example get repo story so you can just download this and get started that's one thing though I recently added this which is that you can in more recent versions of poetry indicate what the package mode is so is something a python package or is the Standalone application I've put this TOA but if you use an older version of poetry this is going to result in error so you might want to remove this line if that happens so once you've added the packages that you need then actually running the application is really easy you just type streamlet run and then the name of the file so in this case I have a Hello World python file and when you run streamlet it automatically opens the browser and then shows you the page this is what it looks like now let's take a look at the hello world file so creating an application like this can be done in just a handful of lines you can configure the title and the layout using set page config that's what you see here so we set a page title and we set a layout of white and there's a couple of other options as well and then when you take a look in the browser you actually see that this is the title of the page so you have the title there's an overview there's a couple of other things here as well and you can also see this when I go back to the code so we have a function configure overview and we call several times SD that's the streamit import. markdown so that basically renders markdown and this is exactly what makes streamit so easy to use because it uses markdown so you have a lot of flexibility with it so we have a subsection here and then we have a couple of paragraphs of markdown and going back to the Brows you also see that right here this is also really helpful if for example you have a read me file that's written in markdown that you want to render quickly on your dashboard but there's more in the code there's another function here called configure available algorithms and euristic this is the different algorithms that we have in our maze generator and the interesting part is here so there's more mark down here but the interesting part is that we have also an expander function and what this does is that it allows sections of your page to be Expendable this uses the context manager syntax so we uh use the width statement and then within that width statement we can do things so in this case you see we get two columns a left and a right column and then we Supply more markdown so on the left we Supply markdown algorithms and on the right column we Supply markdown for the euristic and then once we have those columns left and right we can simply add more markdown to it so in this case we're going through a constant that's a list of different algorithms that we've defined in a constant that we've def find another file and then I simply use markdown to render that and same for the heuristics and then in the main function I simply call these configuration functions and that's it and you can also see this if I go back to the browser so I have here my expander and when we open that you now see we have two columns with algorithms and with juristic and you can really easily change the number of columns that you would like for example let's change this to three columns and then we can going to do left middle right like so and middle of course now we're not using and then let's start the application again now if I open this thing now you see that there is more space in between so we now have a middle column so this is really easy to configure and to set off but of course at the moment we can't really do anything with this right it's just a passive list so it's not yet very interesting so let's now visualize something here I have another version of this same file but now I've added a function to create an actual plot and the nice thing about streamlit is that this has support for math plot lit which means that you can plot your data and render it directly on the web page the only thing that you need to think about when you combine matplot lip and stream LD is that you don't plot directly using the global map plot lit object instead you should create new subplots and then these are going to be threat safe so how do you do this well that's in the create plot function so first we're going to gener a maze and then solve the maze so this is going to generate a random Maze and then it's going to figure out what the correct path is through that maze using a particular algorithm and euristic these by the way are taken from a library called py Wanderer this Library by the way is written by Mis articulate python who is an active member on our Discord server now we generate the Maze and then we solve the Maze and then we create a plot that shows the maze with the path as the solution to the maze so these are all taken from this Library it doesn't really matter exactly how they work but then this returns a figure that's a figure from mat plot lip and the only thing you need to do then is because streamlit integrates directly with M plot that we simply call streamlit do bylot and you supply the figure and that's it so let's run this simple plot and see what that looks like so now when I scroll down we have this nice little maze it's actually not that little but it's a maze and it's all that matters and we also have to pass going through the maze so the black lines here are where you can walk so as you can see this is pretty big unless you have a container object like a column or an expander this is going to fill the width of the page now this is nice but what if we want to get some input from the user for example selecting the algorithm to decide how we're going to generate and solve the maze speaking of input from users how do you actually like to use streamlet have you published any of your applications let me know in the comments below so let's take a look at another version of the code where we're using yet another element of streamly which is the sidebar and this is extremely simple you can just call streamlit do sidebar and then add elements to that and that's going to create a sidebar so what does that look like I have yet another function here called configure sidebar and the only thing I'm doing is adding things to streamlit do sidebar and then that's really all you need to do then once you add those things it's going to display the sidebar and by the way this object this cybar object uses the composite design pattern that means that most of the functions that are available on the mainstream L object are also going to be available on the sidebar things like columns other types of containers any type of object that you would like you see that I add a number of different elements to the sidebar for example there's a seed that we can enter there is a width and the height of the Maze and several other things we can also Define a number of rooms so these are like empty spaces in the Maze uh we can provide a range of these room sizes and then return a maze configuration object so this is all interacting with the maze generation library and then finally create plot now gets a maze configuration that of course taken from the values from this sidebar and as you can see this works really intuitively so here we don't have to worry about passing along this information we can simply do it in code so we configure a sidebar we get the config ation and that we pass to creating the plot and streamlit is going to take care of all the updating and user input handling on the background and the way that this works is really simple and intuitive for example number input is going to return either an INT or a float and this is the case for all the different types of input objects so when you run this version with the sidebar then this is what it looks like so we now have all these paramet here and for example I can let me scroll down a little bit so here we have the maze I can change the seed and that's going to create a different kind of maze or I can change the width now so let's make it a bit wider and then this is what we get or we can make it flatter Let's see we can make a couple of really weird mazes this way you can even create a number of rooms so those are basically empty spaces inside the maze and you can also Supply a range of the particular size of the rooms that you will allow in the Maze so quite a few different options and we can control them directly with these sliders and elements here in the sidebar and we don't have to worry about how everything is being rendered and updated because stream takes care of all of those things and by the way if you're enjoying this content you might also like my free Weekly Newsletter called the Friday Loop where I post regular updates on what's going on with iron codes as well as more free content and news go to the Friday loop.
com to sign up now at the moment we can only show a single algorithm and euristic so let's add a bit more control and for that I have yet another version of the code with a couple of more controls in particular in the configure sidebar function we now have a multi- select element here so with streamlet you can create controls from data and this enables you to gather in this case algorithms and humanistics from this library and then present them to the user and the multi- select object allows you to format how this value is going to be presented so let's load this application again we have our little maze but now we have the multi select here so when I open this then we see we have a selection here so for example let's say we want to do swarm and I'm going to remove D extra and the euristic we can use cosine similarity and now different algorithm is used to find the path in the Maze so these multi selects really help you control the output of your dashboard now if you want to publish your app there are three simple steps steps that you should follow first you need to make sure that your app is hosted in a public GitHub repository with requirements. txt for the dependencies and then you sign in to share.
Copyright © 2024. Made with ♥ in London by YTScribe.com