But if you know where things will differ the migration will go much more smoothly. Migrating frameworks in an application can be a daunting task especially if you don’t know where things might go wrong after copying and pasting code. get ( "/logout" ) def logout ( request : Request ): #. With that said you can put the templates folder anywhere, but you’ll need to instantiate a templates variable for FastAPI to pick up on it like so:įrom fastapi import Request from fastapi import RedirectResponse #. This will only vary if you are using Blueprints because you will need to pass the path for the templates to the blueprint you create.įastAPI on the other hand, not only you do have to make sure to install Jinja2 on your environment, but you also need to tell FastAPI where to find the templates. You can even have two formats: either as a module where the template is on your root folder or as a package where the template lives inside your application folder.Īs long as you use a templates/ folder Flask will automatically pick up on it. You can follow the links below:īoth Flask and FastAPI support templating using Jinja, but there are a few differences.įlask will automatically identify templates in your project so you can use the render_template method and Flask will look for the template in your project folder. If you are interested in reviewing the code, both applications are available on GitHub. So once I started migrating things over, I realized there were a few things that I would need to adjust for the app in FastAPI to function properly.īelow you can see what you’ll also need to adjust if you have a similar application you want to migrate over from Flask to FastAPI.ĭisclaimer: Since the authentication guide in FastAPI is not public yet, I have created two sample applications with all the code shown in the examples of this blog post. “ How hard could it be?” I know, I know, famous last words. Adjust anything necessary for the Flask code to work the “FastAPI way”.Copy templates and styling as well as any JavaScript.Reuse as much as possible of the original app built with Flask:.To make things easier on me, my plan was: So I decided to use the sample built in Flask and migrate things over to a FastAPI-powered app. Thinking of the work that I do, I could use the new version built with FastAPI to write a guide that would help developers understand how to protect their applications built in FastAPI using Auth0. While writing some code to answer a question from a developer a thought popped into my mind: “ Since Auth0 can be used with any framework, why not create a version of the sample app using FastAPI in the backend instead of Flask?” It is secured with Auth0 to simplify the authentication and authorization processes. This app is a typical web app, consisting of a backend built in Flask, a frontend made with Jinja templates, and some CSS. This demo application showcases how Flask and Auth0 can be used together to develop web applications. For example, I used a sample app built with Flask, Jinja, and Auth0 to build a demo for sponsored booths. The web app that started it allĪs a developer advocate at Auth0, I build sample applications that can be used to showcase our product. Are you migrating from Flask to FastAPI and facing problems? In this blog post, you will learn about some of the issues that you may encounter when migrating a web application from Flask to FastAPI, as well as how to solve them.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |