Flask Webcast #1: Intro to Flask

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay I think it's time welcome everyone thank you for for joining me on your Saturday thank you for setting some time apart to to listen to me so first a basically the idea that uh you know that came up to me and and the reason why I started this I'm thinking I'm gonna be doing one of these live events uh per month and each month we can think we can decide together I guess a topic a flask related topic to to discuss now since this is the the first event things are going to be a little bit I guess rough around the edges i I never done this before so I apologize if things are you know a little bit a little bit strange and you know not super streamlined but anyway that's that's the idea if you look in the in the comments section of the YouTube page where you're watching this there's just a link to a gator chat if you want you can join and chat with the other people that are watching while I do this I see that there are there are a few already there and it'll work and I'm also going to use this this chat the Gator chat for for you to submit questions and I'm going to tell you a little bit about that in a moment what else do I need to tell you as a way to to thank you for for joining me today I put the flask make a tutorial video and on discount so if you were thinking about getting it you can get it get it cheaper today so it's it's my little way to appreciate you for for joining me here okay the way this is gonna go is as follows I have it there are three questions that that people ask me a lot and I'm going to start by answering those three questions and two of those questions are sort of technical so it's going to involve me showing you code so they're not simple questions and that's going to be the the main content of today's live stream and then doing doing me showing you you know all this stuff you may come up with questions and what I want you to do if you have questions for me that I'm going to answer at the end after I'm done with with my presentation is to put them in the giver chat and the way you need to do it I have a magical contraption here that's watching the chat for four questions and the way you need to do it is to prepare your question with the word question and then a cone so I'm going to show you I'm going to post a question myself so that you see how that needs to work so question : and then type your question and hopefully you can see it now in the chat and that's going to put make make the question available to me so at the end I don't have to search through the chat and instead I'll have the questions ready for me to answer so that should be pretty simple hopefully and they the way I'm doing this is actually part of the demonstration so I'm going to talk about how I'm doing this this thing because obviously I do everything in flask and this is no exception so I'm going to talk about this too so I think I'm gonna get started so the the three questions that people ask me a lot these days okay so number one is if ii sorry so question number one is if flask is better than jungle believe it or not people ask me that alone so i'm gonna expand that question and change it a little bit make it a little bit more politically politically correct and i'm going to change it to why flask is the the best web framework I'm gonna change it like that so I'm gonna answer that question the second question that I get a lot is if flask is able to if you can use flask to build really large applications people think that flask is only for for toy applications or small applications so that's going to be the second question and these two are sort of fairly technical so I'm gonna leave them for the end the third question that people ask me a lot and this is this is only in recent times is which of my two books is best and that is you know it depends on yourself so you probably know that this this has been around for for like three to four years and now there's the second edition that that came up like a week ago and I also have this guy which is the eBook version of the mega tutorial and you can see from the start if you want to compare you can see right there there's a difference and here's another difference so the makeup tutorial is substantially bigger has more content and the reason for that is that it's self-published so I get to do whatever the hell I want with it the the O'Reilly book has some restrictions on size that were imposed by the publisher so I had to find a condensed set of topics to cover in that book in terms of content the the O'Reilly book the flask book from O'Reilly is more specific about flask the mega tutorial does cover flask of course but it also includes a bunch of other topics that you are likely to need as well for example full-text search Ajax there's a fair amount of JavaScript which of course if you're doing a web application you cannot avoid so the bigot tutorial covers a wider range of topics the the O'Reilly book covers a smaller set of topics but it does so in more detail it goes more in depth in those flasks in the score flask topics so I would say that that's the basic difference the mega tutorial book is more accessible to beginners it starts very easy so the first half of the book is probably more friendly to beginners than the O'Reilly book but the second half where I go into all these other topics that include JavaScript and text search then it gets fairly complex so so you can start with the mini tutorial and then as you gain experience you will be able to take advantage of the more advanced topics at the end the O'Reilly book is I'd say I'd say it's intermediate level all through the book so yeah I think that's that's all I'm gonna say okay so I took a quick look at the chat so Torsten please please vote for your question prepare it with questions so that I get it if not I'm not gonna get it so okay so that was the third question I'm gonna go to the first two and let's see so the the first question is why flask is better so I'm gonna go to my screen so this is going to be a a very beginner introduction introduction I'm going to give you the reasons why I think that that's true that flask is the you are the best framework now interesting great okay so for the questions sorry this is why I said that this was gonna going to be a little rough around the edges so there you go that that's what I'm seeing for for the questions you can see it right there and unfortunately I was hoping that you will be able to vote on it but yeah it looks like there's a maximum of the number on the number of connections so some of you probably get to see this if you connect to this website but not all of you which is unfortunate I didn't plan for that so anyway I'll worry about that later so okay let's get rid of this okay so so why flask is the best framework so the main reason is that if if you are a beginner on on web development if you don't know much about it flask allows you to get to a to an application very quickly there is not much setup that you need to do so what I'm going to show you is how how that works and I apologize if if you know a little bit of flask this is going to be something that you already know but I promise you that the the second question or third actually the third question is going to be a little bit more more interesting so I'm going to start by creating a virtual environment and this is how most people in Python create a project instead of installing installing flask and other stuff that you need in your globally setup Python interpreter the the the best practice is to create a separate environment a separate copy of your Python environment where you can install stuff specifically for for your project so then each project gets its own separate environment where you can install things so oops so for that I'm going to do Python 3 this is this is my Python interpreter in your case it may be called Python it depends usually Python 3 will give you the interpreter that you want and then I'm going to invoke the VM package from Python and then I'm going to create a virtual environment with their name VM which is what I always do and for this the second VM can be anything you want it's the name of your personal your private copy of the Python environment so there we go so now if I get my directory here I have the end ignore this thing that comes later so what I'm going to do now is I'm going to activate the environment and for that I need to the source and then activate this is a little script that comes with the a the virtual environment and now you can see that this is activated and that means that when I run Python now it's going to be this this private Python that I have here not the global Python so that's that's all set up so now I can go ahead and install flask so pip install flask will give me flask and a bunch of other things that the flask uses ok and now I can I can start writing my application and here is the the big difference between flask and other bigger frameworks in in those cases you have to start thinking about configuration and you have to create a bunch of files URL routing you know a bunch of things so here all you need to do is write a script so I'm going to create a script that's called simple dot pie and this is going to be my application for this example I'm going to use vim which is a it's a text editor you you can use whatever editor you want or if you want something more more elaborate like pycharm or Visual Studio code that's all fine anything that lets you write text will work for this so there we go we have an empty file and what I'm going to do here is think in terms of what I want to do and what I want to do is that when I type a URL here in my browser I want some Python code to run and then the result of that I want to appear in the page so it's a simple simple thing to do so I'm going to start by writing the function and the function is going to be it's called hello and all I'm gonna do for now is return what I want to display in the page and this is going to be I'm going to use HTML here because we're gonna be viewing this in a web browser so there we go so now I have a function and what I need to do now is hook it up with flask and the browser so I'm gonna start by importing the flask class which comes from the flask package so flasks with small F is the package this is what we installed what I just installed with PIP and then flask with a big f is the main application class that comes with the with package and this is the object that represents your web application so I'm going to create an instance of this class and the way you do it is like this you have to create an instance of this this flask class and as usually as an only argument you pass this dunder name variable from from Python this is this is a variable that Python sets to the name of your package so in this case it's going to be this simple type I name the way that the reason why flask requires this is so that it knows where your application is because more complex applications will need to load other files that are located on disk on the same place as this file and this is going to allow us to know where where that place is so there we go now we have an application and what I'm going to do is I'm gonna say a mapping between the hello function that I wrote and the URL that I want in the browser to trigger this function to run so that it's done with app crowd this is called a decorator and here I can write the URL and for example I can set it to slash and that's going to be the main URL of the application I'm gonna say this there we go so this is all it takes so now the the top level URL of this web application is going to be exposed through the browser I'm going to quit here going back to the command line and I need to tell flask what my application is so this simple tip I needs to be it's actually the only bit of configuration at this point that you need and flask takes that in an environment variable so I'm gonna set flask up to simple type I and with that I can say flask run and there we are the applications now what's going on here so I see I have another thing there so ignore those and now okay talk about rough edges there you go that that's my contraption for for the questions awesome you know what I'm gonna stop this I'm going to change it I'm gonna put it on a different port so here I am telling flask to to run my application on 5001 so now I can hear go to so you can see here the URL so flash tells me what the URL is actually I'm going to comple paste it and that's my hello everyone so if I want now to to add additional you know some other behavior like I can write another function let's call it index and for this one let's say hello world it's so slightly different so for this one I'm going to say up route slash index so this is a different URL so now we have two two URLs I'm gonna run the application again so now this one is hello everyone and if I go to slash index then the it is the other fashion that runs so so with this you are creating a mapping between URLs and Python functions that run normally they will do some sort of calculation and then they will return the contents of the page what needs to be displayed in the browser so so that is what I think is great about flask that with almost no setup you can get something going so of course the rest is going to be the logic of your application and that comes from you so so really the framework is really an obstruction it stays on the side it allows you to create this link between your functions and the web browser or actually any other HTTP client which the browser is not the only one so there we go that is that that's why I think flask is is great most of the framework will will have to start giving you introductions about things how to configure things create data structures for for these mappings between functions and URLs and in flask this this is done simply with the with this with this construct that you put above your functions which is in case you know this is called a decorator that is this a design indicates that this is a decorator it's a Python feature for those that don't know that allows you to decorate I guess apply additional behavior to existing functions so okay that is why flask is it's great how are we doing on questions okay that's good someone voted so someone got access okay I'm gonna answer these after I go through my last question the third question which is why people are actually if flask is appropriate for building big applications so yeah what a lot of people in my view seem to ignore is that if you need to write a big application you need to know how to write if the application was big that means that you're gonna have to write a lot of code and that's the same regardless of you know Django flask pyramid you know whatever right so so big applications are difficult because you know you need to write a lot of code and that that's difficult so in terms of the framework that you use really the function of the framework is to to enable this thing that I just showed to connect your Python logic with with URLs that clients can trigger and flask does this jungle as this pyramid does this bottle does is they all do it they do it in different ways some are more complex and others but at the end of the day you know all that matters is that your flask your your Python logic is exposed as a set of HTTP endpoints when you work with a big framework then there are a lot of rules in how to perform this mapping when you work with flask there are actually no rules you can do whatever you want you saw me just write an application that it's it's just a single file couple of functions and and then I can continue growing it from from this and say if one day that this one file becomes too large and I find it that it's kind of inconvenient to work in such a large file then I can go ahead and split it in two files and keep going and you can mutate the structure of your application as your needs or the size of your project changes so so that that's one big reason why I think really the the choice of framework that you use if you want to write a big application is relevant because really the framework is such a small thing right the main thing is is your application so there you go so some people think that because flask has almost no rules right in particular respects to the file structure the way you structure your project that that's a deficiency or it can not a plus or a pro I see the other way I typically start my all my applications as as this example as is simple to apply and and then I I grow them you know from from here so so yeah I see it as a strength the fact that I a complete understanding that this is all there is so this this is and our application and if I want to split it in two I just do and everything that he needs to work is in my view super-powerful so what I wanted to show you in terms of this idea of growing applications and you know making them bigger is this this little thing that I built for for the questions so this is gonna show you that I have basically I'm flying by the seat of my pants here I have no planning so yesterday I was thinking that I was gonna see the questions that you that you post to me and searching the chat I thought it was gonna be kind of you know not not ideal so it was like 4:00 p.m. yesterday and I decided to build an application to basically monitor the chat and extract the questions and put then put up here so you can see that this is updating on its own so that this is this is it it's not a big application but it's the type of application that that you think today in terms of you know most businesses built this is a single page application it has a flask back hand and then it's it's JavaScript on the client that and that's a fair amount of work as well so so yeah I'm gonna show you this so let's see so I'm gonna show you the the Python side so that the flask side I'm not going to show you the the JavaScript because if we do then we're gonna be here for a long time so maybe I'll leave that for for another of these these things and maybe we can talk about JavaScript from a from a Python point of view so this started like the previous this simple top by example he was a single file it remains a single file but it is at a point where I will start thinking about breaking it in in to at least into separate modules this application has two functions one is to monitor the chat where you are writing your questions and then the other is to provide an API for for this client side that this stuff that's running in the browser that basically the client is constantly asking the flask server hey we have any questions any new questions or hey did any of the votes changed on on these questions and and then the flask project responds with all the changes and then in JavaScript all those changes are added to the page so let's take a look I'm going to show you first let's let's look at the the chat portion so get ur so if it is this chattable using it's actually very friendly it has it has an api so you can send a request to api gator dot IM /v 1 / rooms and that group that will give you a list of all the rooms that you have and to authenticate you need to pass a token now a little bit so for this I'm using requests this is a Python package that sends HTTP requests so here we have this is a flask server but that this server needs to send requests so it also functions as a client sometimes so it says we are a client to gator and it you need to tell Gator who you are and for that Gator gives you a authentication token you need to pass in this this header on the erequest so this this token is in app config and that's that's one interesting an interesting part of flask where anything that you want to configure because you can just toss it in a app config dictionary and then it is available for the application to use so here this is my token and because I don't want you to see my token I never write my tokens in the code so I have it in an environment variable that that I'm importing here that reminds me that for those of you that are going to be at PyCon us in Cleveland in what is it a little bit over a month I'm gonna have a talk there specifically about how to deal with what are the best practices to deal with passwords and secrets so that you never make the mistake of writing it in code and then accidentally putting it on on github or you know your your source control so anyway if you're going there I look forward to see you for that talk but anyway I had configured three things related to to this part of the application the token then the room that I'm going to be watching which is this room that we are all in on the chat and then what's the prefix that I'm going to watch for to to find the questions so then this one shown it's it's a basically that launching is from line 36 to 1966 so it's actually fairly fully short I sent a request to get the list of rooms and then the responses in JSON so I can get the I can get that converted into a pie on lists by calling requests that the JSON method on the on this response object and then for each of the rooms I check if the name matches what I have no config and that's how I can obtain the ID of the room which is a sequence of characters right it's it's a an opaque ID that Gator assigns to each room so this is how I find what my room ID is once I have my room ID I can subscribe to to the feed the feed of that chatroom and that is done by sending a request to string Gaylord Rin slash we want slash rooms slash the ID slash chat messages and I need to send the same authentication token and I'm gonna tell requests that this is a stream so it's going to provide the contents immediately it's not going to wait until the request ends it's going to basically send me a live feed of all the messages that are coming in that you're typing and then I either iteration over these which come as different lines so I can do either lines and then for each line is a JSON object so I decoded to a data structure that I store in message just in case to be extra safe if there's any errors I ignore that line and keep going and then I check the HTML version of the message that you typed and if it starts with the prefix that I have configured then I create a question object this is the the object that are using my database to store other questions and here I set the question to the HTML so this is this is the HTML version but I'm here I'm doing a little bit of string manipulation to remove the prefix so I'm gonna write the question starting on the character after the colon and and then I also write the name of the author the the the nickname for the person who wrote the question and I said the world's to one initial the initial value and then this the the self three lines is what writes the question to the database I need to add it to a database session and then commit it this this DB object is not part of flask I'm gonna go all the way up for this I'm using a flask extension a very popular one of the most popular extensions that's called flask sequel alchemy and this is an extension that provides database access for for a number of databases relational databases there are different extensions for for different types of databases so you you're not limited to using this this this one this is the one that I like but that that's another big benefit in flask flask makes no assumptions about what how you want to work with your data so you can do whatever you want if you want to write a text file instead of using a database you know you can do it flask does not care unlike other frameworks where the database or storage layer is embedded in the framework so if you need to do something else it's kind of difficult so there we go this is my database object this is my question class here I define the three attributes question author and votes and then two to make the relational database happy I also need to add a unique ID and this this class has a method that converts the object to a dictionary and this is what when you build applications of this type that's useful because that's how you can get those those objects sent to the client using JSON which is these days the most popular content type of format to to send data from from server from back-end API so so here I basically create a dictionary that has all this all the fields and I have an extra one here that keeps track of your your votes okay so high level that that's how the first part works and then there's the part of returning all the static data to to the browser when when the browser asks from the JavaScript side and that is or actually I forgot one one little thing so that this is going to be a background thread so I'm gonna run this in the background it's going to be running in parallel with the web server so in case there's a crash you know who knows what happens I don't want this process that monitors the chat to end due to a shady bug or maybe a service disruption disruption on the gaiter segment maybe they go down I don't want this thing to go down so I created a this Gator thread wrapper that it's a white through so it'll iterate forever you know it'll run the function it's protected with a try except block if there are any errors I just print the error but then the Y through will cause the the function to run again so it'll restart and likewise if the function for any reason maybe a bug or whatever if it ends I want to be restarted I want this to never end so this is this is a little bit of extra safety that that allows me to keep this in the background running all the time so let's see let's talk about this one so you've seen this before I showed you this in the previous example in this simple top I sew up the route is the the mapping between functions and URLs Flass has a bunch of others and here's one that that's going to run right before the first request is received so this is a place where sometimes you can put code that initializes things so here what I did is I create the database and then I create my thread which is going to be based on this function the wrapper to the the big function that does the monitoring of the chat I'm going to set it to demon mode and then started in the background so this is gonna run from the time the application receives the first request it's going to constantly be looking for for the messages and watching for questions and then after that we have actually only two two routes here so slash API slash questions is the one that the that the the client can send to to get a list of questions so it gets the entire list of question here I have a little bit of I like the way using flask and flat sequel alchemy you can do a lot in a single line some people don't like this and it's totally fine you can write this in a more lengthy way but basically what I'm doing this is I'm using this Python thing called a list comprehension to generate all those dictionaries with the two dict method that I showed you before on the question object for basically a query I'm running a query on all those questions I'm ordering them by the number of votes in descending order so the question where the most votes comes first and and then for each of those questions I generate the dictionary version and then all of that then is converted to JSON with this function that comes with flask which basically takes a Python data structure and returns a JSON version of it to the client so there we are this is this is how the client can get a list of questions and then then you can vote on the questions so of course since I wrote this question I'm gonna let's see if it works yeah there you go so I voted the question not now it's it - so each time you click on the thumbs up and I apologize those of you that that cannot access this this side that was an oversight on my side I as I said I wrote this it took me about three hours yesterday and it's running on my machine and that egg rock URL that you're going to it's basically routing the request directly to this laptop from where I'm presenting and I didn't realize that they have this egg rock service that I'm using as a limit on the number of clients that can be connected at time so for the next one and pretty sure I'm gonna have time to deploy this you know in the right way and then this is not going to be a problem so anyway let's go back to the code and for for the voting basically what I the client needs to do is needs to send a request to slash API slash questions slash ID and the ID is this this identified that the database assigns to each entry to each question now in this case I'm going to use a post request typically this is something of HTTP HTTP protocol when when you when a client wants to get data it sends what's called a get request and when the client wants to modify or trigger some change in the in the server in the backend you usually use a post request or one of the other ones there are others for making changes and for deleting as well so this is going to be a post request and this is this is something that I I haven't shown before you can make URLs that have a sort of a wild-card you can make you can put anything in any integer here and this is still going to match this function so flask will will send any URLs that start with slash API slash questions and then end with a number to this function and the number is going to come as an argument so I now know what's the ID of the question that's being voted so so then what I do is I recall the vote in this this place that's called the session this is this is a storage that flask allocates per client so if all of you are connected then flask is going to create a session for each one of you and and then the server can write data on that session and that's going to apply just to you so this session and the different sessions for the different clients that they never mix up so I can write in this session which which IDs you voted on so here what I do is I know the question from the database and this is how you you run a database query and then session which this works like a dish dictionary so session votes it's a list and then I add this ID to that list so I keep track of all the IDS that you you voted to on your on your browser and I increase I increment the the volt count on that database object and then right back to the database with the updated vote count and and then in this case there's nothing to return so I return an empty string and here I can do this this number here it's the status code which is an optional thing in HTTP you can you can return a status code that provides additional information to the client and the the standard one so in this case where I don't use a predefined you know I let flask use the default that's gonna be 200 which is the code for okay this request was successful and 204 is a code for okay but I have nothing to tell you so this is the circle that you use when the request was successful but there's nothing to provide in their response and in this case that's for that reason I provide an empty string the I think the last bit that I didn't show is this other decorated function it's called before request this is gonna run before any requests so whenever the client sends a request to this one or this URL either one flask is going to first run this function and this allows me to to do common generic stuff that applies to to the entire project or sometimes the subset but in this case it's due to all the URLs and what I do here is I make sure that this world's list the session volts list is created so if it doesn't exist in the session I created as an empty list and that way when I come here to the volts I don't need to worry about checking if this exists or not it's already created so there you go I don't have this published I bet some of you would like to see this and I promise in the following weeks but before the next one I'm going to try to have this published and that will also give you a chance to see the other JavaScript side if you're interested and in case it carries it's a react application I don't like react and I don't like angular but I I forced myself to use react to see if I if I can like it so far it's not going well but anyway so what I'm gonna do now is refresh and I don't know if if those of you who voted would like to exit the site and allow others to connect and I mean if you want to cast additional votes feel free I'm going to start answering questions from here and we have we have about ten ten minutes to two to the hour so probably do that so I'm gonna start from from the top what is the new what is new in the second edition of your ugly book okay so sorry I'm gonna switch switch back to my beautiful face so that you see me so what's new in this guy so this is the the first edition I don't have the second edition paperback yet you can tell because the the new book has a black bar here that reads second edition so make sure you get the the second one this one is no good anymore so there is not a lot that's different on on the second edition the the main thing is basically I went through the book and refreshed everything so that it is up to date with current versions of flask flask was at if I remember correctly was 0.10 when when this first edition came out four years ago and now we are at point 12 - I think the only really big thing that changed is the the CLI the 0.10 version does not have a CLI in those days people used an extension to create command-line interfaces it was that was called flask script and this book the first edition uses that that extension the new book uses the CLI so I removed any usages of flacid script and and replace it with the CLI the other big change is I added a section on docker containers and how to containerize a flask application but kudos ace doctor did exist but I don't know I guess I wasn't really that much exposed to it I think containers and micro services you know all those things you know started to take momentum after the first edition came up so there's a section on how to work with docker and then there is a lot of little changes all over the place little improvements basically I touched every every single chapter so all the chapters have changed in small ways if you want to know if you should buy this version if you have the first edition I don't know probably not unless you are really interested in the new stuff that will be my advice okay better to order the mega tutorial from your website and Amazon more for money to you yeah absolutely yes yeah yeah so I honestly I put it on Amazon because you need to be on Amazon so that reviews and that's basically the way the popularity and the the usefulness of a book is measured these days unfortunately but it but yes I'd make very little money among the books that I sell on on Amazon so yeah okay this one I don't need to answer if session is a unique producer does it mean if I will vote for a question clear myself we have a hacker here yes for this project there the way I designed this project in three hours yesterday night absolutely you can clear your session and then you will be able to track the system and vote again yes so when I complete this application the votes are going to be stored in the database there's going to be a use of database so it's not going to be stored in a session which is very easy to raise the for those I don't know the the this this session object that I'm using is stored in a cookie so it's actually you you have it all of you have your own sessions so the cookie is a cryptographic key sign so you cannot fake the contents of the cookie but you can delete it so if you delete it then the next time you try to vote the server is going to think that you're new that you never voted so definitely so yeah same pose congratulations next could you recommend a place where I can learn just just enough of JavaScript the kind of applications needed for a web developer well unfortunately JavaScript is a difficult language in my opinion there's there's a book it's a little bit old now it's called JavaScript a good parts I'd like that book basically until it tells you it sort of promotes a subset of the entire language that is more sane to use there are some things in JavaScript there are kind of crazy so that would be something I would recommend and then yeah I don't know I think these days where people use you know these dis of single page application frameworks like angular react a lot of people don't spend enough time learning the language that the JavaScript base and and that's why I think for many people it's difficult if you start from react or angular you're gonna have a hard time so I suggest you you install for example nodejs which is a version of JavaScript that you can run without a browser you can run your ass you can even write server-side backing applications in a way similar to the kind of things you can do with flask and learn the language and and then I think when you then take on the frameworks things are gonna look we're gonna make it a little bit more sense okay next have you ever tried depend for manipulating virtual environments and packages if yes what do you think about it yeah I did I don't like it I think it's not ready for production use I think it shows promise but but I I don't use it myself I actually I can show you so so the the great thing about PPF in my opinion it has a number of things there are nice but the the most important by far things that people ask that when you work with out people you don't have is a much clearer way to manage your requirements by having the PIP file where you specify your dependencies and then the log file where you have all the dependencies with the exact versions and then all the secondary dependencies so that that's that to me it's the reason why I would use Pippin but we tried it at work actually and I tried it myself for my own stuff so Pippin is it's slow it shells to pip for everything that in installs so it takes consistently longer than the tip to install so that's something if when you're doing this for for real only you know on a production project you you know probably have a build script that runs automatically when someone makes a change and we have that at Rackspace where I work and we found that it takes a long time basically it adds time to the build and also depends if you all you need to do is go look at the if you go to the repository on github and look at the commits you're gonna see that there's a lot of activity it's changing all the time the the the developers have this style of making commits direct to master they don't use pull requests they know use code reviews so in our experience we used it at Rackspace for about a month and then decided to stop and weave in in that month or builds broke I think three times because of changes that went that went into the official releases that are the hot box so it's definitely something something that I'll continue to monitor and we'll probably advertise it at a point where I consider that it's safe to use but I think if you use it for for small projects is it's fine so I mean if you like it and you know you don't have really big requirements out of the tool then it's probably fine and I think it does a good job but but yeah I'm not using it yet what Falasca applications do you develop at Rackspace okay so we do at Rackspace I'm sorry I was going to show something I'm gonna go fast sorry for this question the PPF question I'm gonna show you what I do instead of using instead of using PDF so so this Q & A this is the application that I just show you so here's the Python so what I have is I have a requirements file and I have a requirement start txt file so requirement ext is a file that I don't edit by hand so I edit this guy so so requirements is the list of requirements I have flask flasks equal alchemy and requests so what I do is I create my virtual environment import this requirements into it and then I do pip freeze and then I redirect the output to requirements start txt and the they basically pip freeze will will show you I need to update my my paper here but anyway ignore that yellow stuff that freeze shows you the the exact versions of all the dependencies that you're using so mm-hmm so that this is how I achieve that same kind of thing that people fuss with the log file so anyway so okay what I do are Rackspace so Rackspace I work in a division that's called manage manage security basically companies pay us money so that we keep their systems secure and the free of any attacks and vulnerabilities that type of thing what I do is I work on some systems that are used mostly internally but some of them are also accessible to our summers we use micro-services which is basically the idea of taking a big application and instead of writing a single back-end for it basically split it split it in multiple parts smaller parts so we have flask micro services so each micro service is based on flask we deploy them on AWS lambda which is you may have heard of this this term that a lot of people say these days server less so we deploy our flask applications to AWS lambda which requires us to not maintain any servers and it's really nice and yeah an area that I spend a lot of time is the authentication micro service so we have we have tokens token generation token impersonation a bunch of interesting things that we use and there are other other micro workers need to use to basically to provide the service to our customers so yeah that's why it okay it's been four minutes over there trying to get to a few more so does the flask bootstrap extension gets updated as bootstrap releases a new version yes as far as I know the the flask bootstrap developer keeps the version numbers on his extension in sync with flask in sync with the bootstrap JavaScript libraries I haven't checked if he released one for bootstrap for I think he was waiting for for bootstrap for to be out of better to do that so it may not be out yet the version that I use or in both of my books is version 3 how is it possible to make some kind of authentication which restricts access to static files like images documents to logged in users this is an interesting question so you there are a couple ways to do this for development you can you know that flask has the static route this is a route that's predefined that serves the static files you don't have to use that you can create your own route so you can create a different route and add any you know flash plugin decorator or flask HTTP off you know whatever you use so they create the function and then to serve the file you can use the flask function that's called send file so that's one way for production what I would do is typically I deploy nginx in front of my flask servers and what I will do then is in nginx you can you can have that there's an option I don't remember the name right now but you can tell it for any files that nginx serves directly any static files you know you can tell it to send a request to any endpoint of your choosing to check for authentication before it serves it so so then you can have your you can have a flask route that's dedicated to verify authentication and yet continue to serve static files with nginx okay next beginner if I were to develop my personal blog understand that I need a form for posting my blogs to the servers that I can be viewed by others how do I restrict the viewers from accessing this form used to submit blogs are you you're gonna need user authentication in that case so that this is covering in both of the books and you can also see it on my blog if you look for authentication posts in the if you look in the blog categories I wrote a bunch about this topic so basically you're gonna have to log in to your own blog and that that's what I do for my blog I log in to my blog and then the blog knows who I am and then it shows me a more enhanced version of the blog it's not what you see when you go to my blog I get additional options in the in the top navigation bar that allow me to to write blog posts next what is the best library or way to use flask with MongoDB okay great I don't know because I don't use myself so I will probably use engine if I had to but I don't really use it's been a while since the last time I use so I would probably have to revisit this and see if there's anything better in the Python world this is something important that you don't have to look for extensions that are specific to flask if you don't want to if there's a great extension and doesn't have I'm sorry if there's a great Python package that doesn't have an extension for flask you can still use it flask is totally fine with it so yes I will probably just to survey the the Python package repository and see what's what's out there and fine fine what works best but unfortunately I don't know off the top of my mind next after we have gone through your mega tutorial how do we keep updated about the new flask features updates etc what I would you know whatever this I will keep an eye on the activity on the flask github repository so technically I'm a core developer in the flask project I don't really participate much in the day-to-day development that there are a couple two to three other core developers that really do a great job keeping track you know pushing the development forward and what I do even though I don't participate actively a lot I have it you know I start the project so I see the changes that go on on my github feed so every day I check what's going on and when I find something that's interesting or something like that I see that it's wrong then I I get involved so you should do that too you should keep an eye on the changes that are going in the other release schedule it's kind of slow there are a bunch of things that are in master for example that are not in the official version so you know if you check those out you might you might find stuff that that's coming that that you you I mean you can install flask from master and start using today because in general the project is very very stable so yeah oh great so oh crap that's awesome okay you can tell that I'm a beginner at this okay sorry I'm an I'm gonna switch to the screen man there we go okay so what I was I think yeah the only thing I showed you is this this requirements thing yeah you were seeing me instead of this screen sorry about that so it's this this this is my requirement start txt file and this is my requirements file so I keep edit this file and then export to this other file sorry about that hopefully you I don't remember when I switched hopefully you didn't miss a lot [Music] that's that's upon what I have no feedback I guess but anyway I'm going to the next one you forgot shows you took my window thank you thank you for make me feel like a fool Thanks can you in the future give an example running flask in AWS lambda sure I could because that's actually what I do I work and okay the next question is related to use Zappa for lambdas I don't so what I do for for the deployment of of our server les flask applications is I wrote a little whiskey adapter so whiskey or WSGI is the the format that applications use to export web applications used to export the application to a web server so what I did is I created an adapter that invokes the application in the same way a web server invokes it and and then the on the other side this application uses the format there that API gateway and AWS lambda use for for the wrong thing it's called the proxy integration if you look it up in the talks so that the proxy integration in AWS serverless it's kind of similar to whiskey in the sense they both have they accomplish the same goal but the format is different so what I wrote is an adapter and that's the same thing that's up at us so mine is it's simpler it's just a little function that uses virtual to create a whiskey environment and then it invokes the application I I don't think this app I work for for us at Rackspace we have our own framework so so I think it's not it's not going to work because zappa not only deploys your function and it set up sets up your domain it does a bunch of other things that we really don't need because we do in a different way so so yeah yeah we have our own thing so anyway and this there's any final questions I think I'm gonna end this I apologize for that for the screen I have to keep track of that so anyway maybe I need an assistant next next time I'm a name and assistant and I'm gonna have him on audio so that if there's any problem I can you know I can get alerted in time so anyway it's a learning experience so next time hopefully it'll be it'll be a little better okay so say switch one more time to my face and yeah I hope you found this useful remember that I have the the mega tutorial on discount on my site which is learn dot Michael Greenberger calm if you were thinking about getting it is this a good time if you have the e-book and want to upgrade to the to get the video which is basically a knee going through the entire tutorial and you can watch my screen that that's also in discount so they upgrade from ebook to videos also discounted heavily so so yeah if you have any ideas for topics that I can do next time one that I was thinking that maybe I do is talk about the application and request contexts in flask which is something that a lot of people get confused so but but yeah if you have any other ideas let me know and maybe roughly in a month from now I'll do another one of these and we'll keep keep doing this so thank you very much for joining and I guess I'll see you next time we have any questions hit me on Twitter or whatever else you find me goodbye thank you
Info
Channel: Miguel Grinberg
Views: 21,099
Rating: 4.9853477 out of 5
Keywords: python, flask, web development
Id: fft6IvL-y1g
Channel Id: undefined
Length: 74min 51sec (4491 seconds)
Published: Sat Mar 24 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.