 It is time for inviting our next speaker, Tonyo Sims. Let's welcome Tonyo. Tonyo is a Python developer advocate at Wunaj, which is a cloud communications platform and is also one of the sponsors of EuroPython 2021. An interesting thing about Tonyo is that Tonyo is a former professional American basketball player who earned entry into the Wisconsin Basketball Hall of Fame. Really good. Today, Tonyo is going to talk about a face-off between two Python frameworks, the new KID, Fast API, and Flask. There's going to be a really interesting talk. OK, so let's get started over to you, Tonyo. All right, thank you for the introduction. I appreciate it. So welcome to the Flask versus Fast API face-off. OK, I feel your pain, right? Like, I understand how frustrating it can be choosing a framework in any language, especially when you're first starting out with coding. It's like a vicious cycle. Like, you choose one, then you want to choose the other, and you keep going back and forth. So imagine it's your 1985, OK, and back in the future was the number one movie, and almost everyone had big hair, including me, actually. So are you a sports fan? If you are, let me know what sports you like and what teams that you like. Let me know in the chat, and we'll talk about it after this presentation, because there is something else going on in 1985 that was really big. I'm not sure if you followed the NBA, but one of the biggest NBA basketball robberies of all time between the Los Angeles Lakers versus the Boston Celtics. It was Maddie Johnson versus Larry Bird, right, two of the biggest superstars. I mean, this rivalry was huge. It was as big as football versus football. And my personal favorite, European cuisine versus American food. But there's good news about rivalries and face-offs. It helps. They help to create innovation, innovative ideas. And instead of competition, you can become collaborators. And I'm happy to say Magic and Larry Bird became best friends. And they're still besties to this day, and have even done commercials together. So you may be wondering, what does this have to do with Flask versus FASAPI face-off a lot? Flask and FASAPI are kind of similar. Both are very high-performing and lightweight, Python web frameworks. They are micro frameworks, which means that they're just stripped down without all the bells and whistles. So with a lot of flexibility, they're very different as well. One is battle-tested, right, and more reliable. The other one is newer. And one comes with lots of built-in libraries. And the other one, you have to install some extensions. So hi, I'm Tanya. And I am a former professional athlete. I've played pro ball in Europe. You can install both sports references. Turn Pythonista. And now I'm a Python developer advocate at Vonage. We are one of the leading communication API providers. You can use our APIs right to build applications using video, voice, text messaging, and much more. Our customers range from those in the telehealth sector to insurance to startups. And we have a freebie for you. You can take a screenshot of this if you'd like. It's a coupon code to try our products. We give you free credits. Use our APIs, and you can build cool stuff. And it's redeemable through August 15th this year. And you can redeem your coupon here at dashboard.nexmo.com slash coupon. So this screen might be worth taking a screenshot of as well. So you know where to go to redeem your coupon. So before we begin our face-off, I want to just set some expectations. So I might go back to the basics sometimes, because everyone here might be at different learning stages. And I'm not sure about you. But when I first started learning how to code, it felt like my head was going to explode. So type in the chat, like, what was your journey like when you first started coding? And how is it going now? I'd love to learn more about you. So this talk will focus on Rust APIs. But first, you'll have to grasp what is meant by API. You might already know, but if you don't, an API is like a translator. So if you speak English, right, and you want to converse with someone who speaks, let's say, Pig Latin, and you can't understand each other, an API would sit in between the two of you and interpret your request and send back a response. And a Rust API is a type of API that relies heavily on something called HTTP. And HTTP is a way to transfer data over the internet using what's called a client server model where the client makes a request and the server returns a response with data. And that response is typically a JSON object, which is used to transport data. So two different systems can understand each other. So if you're familiar with Python dictionaries, which I'm sure you are, JSON kind of looks like a Python dictionary with key value pairs. Okay, so let our face off between Flask and Fast API begin. Let's start with the Helloward application to get you up and running quickly. So there's two things to understand running your Helloward application. The first are decorators and decorators are just functions, okay, which modify the functionality of other functions. And they start with the at symbol followed by the name of the decorator. So in decorator, you can think of it like decorating a cake, right? It starts with something, something existing, then you modify it and you add the frosting, the flowers, the sprinkle, the drizzle, whatever you like on your cake. The next thing to understand is a route. And a route just maps a function to a URL. So it knows which webpage to take the user to who's making the request, okay? So in Flask, the Helloward app would look at something like this. You'd have your decorator here and it's routing or mapping this function home to the URL. In this case, it's a backslash, which is just the root or the index page. And returns, hello world. And it gets served up on port 5000 by default and you can always change the port and returns hello world here in the browser because it's doing a git request. Similar to Flask, hello world and FastAPI, the decorator is slightly different than Flask. Here it is. It's just the decorator at app.git. And in FastAPI, the HTTP verbs, git, pulse, put, delete, have their own decorators, which is really nice. And it just turns a dictionary with hello world here. And then the browser gets served up by default on port 8,000 in FastAPI and you can always change the port of course and it returns hello world in the browser. So both require a little coding to get a hello world app up and running. Then the only main difference is the way the decorators looked. In Flask, you know, you have the at app that routes and then in FastAPI, you have the at app.git or git request. So running and development, right? Running locally in your machine first so you don't put error, prone code in production because people are gonna freak out a bit at this. Well, people always freak out anyway, right? So to run development in Flask, you have to export your main Python file, which in this case is app.py like this. Then you run the Flask development server by running Flask run. This is all done in your terminal. But what if you wanna make a code change? You'll have to keep restarting the development server every time and in order to keep the development server running, you'll have to do something else from your command line. You'll have to export development into the Flask underscore ENV variable. In FastAPI running and development, FastAPI uses something called hot reloading. And hot reloading keeps the app running when you're making code changes, right? So you don't have to keep restarting the development server over and over again. And what you can do is from the terminal, just pass in the reload flag. And to start the server, you just would type uvicorn main colon at main is the name of your Python file that you're running. So in this case, it's main app py. And you can think of uvicorn as this like lightning fast server that allows your application to perform faster. So hot reloading saves you lots of development time because your application reloads automatically anytime you change the code, which is pretty sweet if you ask me. Okay, now you should be warmed up, right? It's you're in the game, it's second quarter. Let's talk about HTTP methods in Flask and FastAPI. And HTTP methods are just action oriented verbs. We've already covered how to do get and Flask and FastAPI, which is like a read-only request. So let's see how to do a post in both of these frameworks and a post you're just creating a resource. It's kind of like you're submitting data to a form and it gets submitted to the server. So in Flask or HTTP methods, you will start with a post request, right? We'll just do a post request because we've already seen the get and here we want to create data and send it to the server. So let's see if we have this list of dictionary called teens. So the difference is in Flask with the post request, you'll have to specify post in the methods list inside of your decorated route. This is the new team that you want to create. You want to add to the list of dictionaries and you return it by using JSONify, which turns Python dictionary into JSON so the server can interpret the data. But if you pull up the browser and if you go to the route, you'll get a 405 method that allowed error because this is a post request, right? So and by pulling up a webpage, that's doing a get request. So you'll have to use an external tool called postman and postman acts as a client. So you can see your post request and the data that you've created in JSON format. Fast API HTTP methods. Fast API provides separate decorators for each HTTP method. And we'll see a few, we'll see an example in a few seconds, but in Fast API, the intentions are clear with having to specify each decorator with an HTTP verb, right? And it also provides native support for JSONify. So it automatically turns a Python dictionary into JSON. And I'll show you why Fast API has an edge when it comes to HTTP methods and routing. And that is because of automatic documentation, which is an interactive API documentation where you can see your requests in the browser, such as like your post requests and interact with them like passing in data to see what you get back. So in your response, it's like magic, honestly, right? It's gonna make your life easier and you don't need any extra tools like postman. So Fast API automatic documentation. Fast API is based on something called pydantic, which is a, it comes with Fast API out of the box when you install Fast API. So there's no need to install pydantic. And pydantic is a framework for easily modeling and validating objects. With pydantic, it takes the pain away because you don't have to write constructors, right? And constructors are things that and instantiate your objects. See, and you also get all the magic methods or the dunder methods, which are like helper functions. Pydantic also does data validation and uses Python type hinting. So you'll get friendlier data validation areas, which of course reduces your debugging time, which is always nice. So type hinting is pretty much what it sounds like, right? They hint at the variable type you're using and they help document code for other engineers that may be using your code and they help catch bugs sooner. So an example of type hinting would be just, you know, this variable player ID followed by the type integer or int. Pydantic also works together with something called swagger UI for the automated documentation, which allows you to visualize and interact with your APIs. So let's do something more cool so we can see the beauty of how automatic documentation works in Fast API. So to use pydantic, you'll have to import something called base model. So our class that you're about to see will inherit from base model. So inheritance works like this, like if both your parents have brown eyes, it's highly likely that you will inherit your brown eyes as well. It's kind of the same with object oriented programming. Works the same way inheritance. So here we're importing base model. Then we have a class called player that inherits from base model, okay? So it takes all the properties and functions and all the stuff that comes with base model. You're declaring your variables as type hints. So for example, player name string, player team string, player age type int. Inside of our function, we have a parameter. And notice we're doing a post here in the decorator. Inside of our function definition, we have a parameter we're passing in. It's called request. That is a type of player, right? So that is a type hint. We're saying this variable request is a type of our player class that we just created and then we're just returning a dictionary. So to access your document, your automatic documentation, you would just go to your local host slash docs. And you can see things like the schema, which is like a skeleton for your model with your variables. And you can see which variables are required with hints that you see like the asterisk, the red asterisk next to them. So those are the required variables or your optional variables. You can try it out. There's a try it out button to test your API endpoints by passing in values for your variables. So here we're just passing in Michael Jordan for the player name, Chicago Bullis for the player team and the player age 32. And then you can execute. You just click execute and it's going to give you the response body back or what the server will receive for the post request. So once again, no need to use postman. You can just do everything right here from your browser. You also get back the curl request, right? Which lets you talk to a server by sending data or making a request through the command line. Generates it for you so you don't have to write it from scratch and I've written curl from scratch. You may have to and it is time consuming. Flask doesn't have any built in feature for documentation generation. So you have to use an extension called Flask swagger and you have to do a lot of configuration as it doesn't come out of the box with swagger. So since automatic documentation comes out of the box with Flask API, you don't have to install anything and Flask API also comes with PyDantic out of the box which uses the automatic documentation. Data validation, which can be client or server side validation, which uses our lovely friend PyDantic. So, Flask API data validation. We are creating a class here called log in. Okay, that once again inherits from base model or the PyDantic model with our type hinted variables and our class username, password, agree to terms. So in our function, let's just check to see if username is equal to Jane Doe and password one to be for five. If it is, then it's a success. Otherwise, it's going to fail. So we've pulled up our automatic documentation again. Click try it out. Let's pass a none for the username, but we're keeping the password the same and agree to terms it's optional but let's just make it true here. So PyDantic is gonna work its magic and you'll get a friendly error message telling you exactly what the error is and here it says error is expecting value which is right on the money, right? Because we passed a none for username and it's expecting a value and we didn't pass it one. Class data validation. Class does not have any in-house data validation support but you can install the powerful PyDantic package for data validation though. So once again, Fast API comes with PyDantic which will give you friendly error messages and reduce time spent debugging. So, all right, it's fourth quarter. We're coming to an end now. Let's talk about like the templates folder. You may have seen this if you've done some web application development. The templates folder stores your HTML files, excuse me, if you want to build a web application and in Flask, the templates folder by default, it looks, you have to create the templates folder in Flask and your file structure and it looks for that templates folder in a quote unquote templates folder that you've created. So it stores your HTML files here. You have to use something called Genja to display your variables in HTML and Genja is a templating engine that allows you to write code similar to Python to display your HTML and it looks something like this like, so here's your HTML file and to display variables in Genja, you have to surround them with double curly braces. So in Fast API, you have to install Genja and you have to define the templates folder in your code. So when you install Flask, you also get Genja comes out of the box which is really nice for that. Versus in Fast API, you have to install it separately. Running in production, going live so the whole world can see. You know, it's scary sometimes, but Fast API in production, you can use something called an ASGI web server. It's, think of it as like a very, very fast web server and it's asynchronous. So asynchronous means that, so imagine this, you have a bunch of like requests coming in. They don't have to wait for the other ones to complete or finish before they start processing. Like they can process at the same time, right? Which makes it, makes for fast processing. And Flask production, you have to use something called WSGI, what stands for, it's a Python web server gateway interface. It's been the Python standard for many years, but that is changing. So WSGI is synchronous, right? It's like the total opposite of asynchronous. For example, imagine that you have a bunch of requests. They have to wait in line for the others, for the other requests before it to complete. So it's kind of like a queue. So imagine waiting in line at the movie theater. I know movie theater is kind of like a thing of the past, but hopefully they'll come back. So you're standing in line at the movie theater and you're at the end of the line and you have to wait for the other people in front of you to get their ticket. So that's kind of how synchronous web server works. So, I mean, ASGI makes for faster performance in your web applications because of the way they process requests asynchronously. So I feel like, you know, Fast API definitely does have the edge here. Oh, there's so many things we didn't get to talk about today because we had a limited time, but I mean, we could have talked about path parameters or you might know them by URL parameters, which returns a single item. So for example, if you want to see the details for one item, it would get the ID for that item through the URL you pass in and return. So for example, if you want to get, you know, you have like this player class and you want to see the details for one player, maybe it's Michael Jordan, you would just pass in the ID for Michael Jordan, which maybe is, could be two, through the URL and then it will return the details for that player. Testing, we could have talked about testing, you know, what pie test and unit test. And then there's also modularity, which is really important. So, you know, Azure app grows, you know, how do you break it down into modules so you can port your code or so other developers can also use those modules too. And Flask uses something called blueprints for modularity and in fast API uses API router, I believe. So my winner, drum roll please. Okay, so this is how I would choose personally. So I would use Flask for this, for these things. Okay, so, you know, as we said, Flask is battle tested. It's been around for a long time. It's reliable. There's tons of documentation. Lots of user communities. If you want to quickly prototype something, you know, definitely use Flask. And if you wanna do some web application development, you can use Flask as well for that. That's what I would use it for. Use fast API if you want speed, right? Not only in performance, but in development time, right? Cause it's, I mean, the way fast API is designed, it's really nice that it speeds up your development time. I would use fast API if you wanna reduce errors in bugs, right, because of the type hinting and our lovely friend, Pydantic. And use fast API for, definitely use it for API development. I really do believe it's going to become the standard in the near future in many years to come. And also for fast API being so new, it has absolutely amazing documentation. I'm really impressed by the documentation that it has and you can find what you're looking for online through a simple Google search or you know, just pull up their documentation online. So, thank you so much for your time today. And also thank you to the EuroPython conference. You guys do such an amazing job of putting this conference together. I know there's a little bit of challenges, you know, with the pandemic, but EuroPython, you guys did awesome. And I would love to hear from you. So, this is where you can find me on Twitter. I'm just at Tanya Sims. Give me a follow, I'll follow you back. And yeah, so you can ask me any questions you want about sports or Python or anything else. So thank you very much. Thanks Tony, it was a pleasure listening to you. We have a couple of questions lined up and I'll just read out them for you. Okay. So, let me just read them as well on the screen. The first question is, basically it's from a fan of basketball here named Sebastian. He's firstly thanking you for the talk and next is the question. So, which framework is magic and which one is good? So, for all the basketball fans out there, LA Lakers and Boston Celtics fans, this question may interest you. That's, oh my God, this is such an amazing question. I love this question. Oh wow, oh wow, this is tough. This is really tough. I mean, like I was saying, like towards the end of my talk, I feel like, I mean, both Flask and Fast Apeye definitely have their pros and cons and so it's kind of hard to choose. I mean, magic and literary bird were pretty comparable, like in terms of like talent, but I do think moving forward in the future, I think I do think Fast Apeye will be the standard especially for like API development. So, yeah, I mean, Magic Johnson was like my favorite player growing up and I also really like Larry Bird too. So, yeah, so that's how I answer that. Nice, okay. So the next question is Flask 2.0 now looks like Fast Apeye. However, the same is not reflected in the docs yet. So what's your opinion about the same? Yeah, I definitely kind of agree actually. I feel like when I was learning Fast Apeye, it was kind of like, it took some of the best feature. Oh, actually you're saying, oh, interesting. Yeah, I mean, I feel like Fast Apeye took some of the best features from like Flask and Django and kind of like stripped it down and scaled it down. And yeah, Flask 2.0 does look a little bit like Fast Apeye. So I haven't played around with Flask 2.0 as much since I've been, more of my time has been spent doing Fast Apeye development using both using APIs, so. All right. Okay, I hope that answers the question. Next question is, why would one want to switch from Django to Fast Apeye or Flask? Are there any differences based on performance or scalability? Yeah, I definitely think, so you'd want to switch from Django to Fast Apeye or Flask just because, so Django is a great framework. They'll get me wrong, but I feel like it's maybe slightly bolded, you know, but it does have a lot of good features as well. So you'd want to switch to like Flask or Fast Flask or Fast Apeye because they're micro frameworks and you have a little bit more flexibility and kind of like the packages or the libraries that you'd want to install. I think too, with both of those Fast Apeye and Flask, if you want to get something up quick, I would definitely use one of those frameworks. Okay, nice. All right, okay, the next question is, based on the community support and documentation available, which one would you recommend, Fast Apeye or Flask? Ooh, oh, wow. Okay, so, you know, Flask has been around for a while now and it does have a very supportive community and it is reliable and has a lot of documentation, but on the other hand, Fast Apeye, even though it's a newer framework, there is, the documentation is amazing, honestly. It's very thorough. If you just go to their documentation site, I was really blown away actually by like, by their documentation and just how complete it was. Now I'm starting to see more questions about Fast Apeye on Stack Overflow and I think the community is really growing as well. So, yeah, I don't know if I could say which one is better. I mean, I think like for, at least for me, I'm gonna be moving forward doing more Fast Apeye stuff, so that's kind of how I wish it was. Nice. Okay, so we are towards the end of the session, just the last question. And it's an interesting one, although you enjoy more, is it basketball or coding in Fast Apeye? Oh, wow, oh, wow. I would say they're definitely a tie. Now, I haven't played basketball in a while, but I love, obviously I love coding in fast Apeye, so I wouldn't be here, but yeah, I would say both. Okay, nice. All right, so that ends the session. So thanks a lot, Tonio, for joining and delivering a really nice talk. Thank you, thanks for having me. I hope people enjoyed it a lot. All right, it sounds good. Thank you for having me, I appreciate it. Okay, bye everybody. So anybody who has further questions can please join the breakout knee room. We're Tonio will be present to answer your questions. And in 10 minutes, we are having the lightning talk starting, so tune to them as well. Thanks everyone and stay tuned.