The simplest solution is to add a location to the static folder by using either. They differ slightly from templates files which I will explain below in more detail. we just get the main static folder from the base blueprint at /static and of course, the file does not exist there. We might expect the URL to be /lists/static/css/custom.css So if we add a template into the lists template folder and use something like So in the nature of blueprints you would then expect the path to this folder to be /lists/static as we are defining a prefix and then defining a static folder location. What I wanted to discuss is how to make proper use of the static folder within the blueprint, as you can see above we define the static folder, to contain our css and js files for example, as being “static” This isn’t a full Blueprints tutorial and if you want to learn more about Blueprints then either go here, here or here you create a folder for example /lists/ and in there you define an _init_.py file or similar that will contain the instructions for this blueprint. You may do some includes to pull in additional functions but that is probably it.Īs your Flask application grows in size this is obviously not feasible and the different sections of code need to be separated out into different folders or separations of concerns as it were.įor this, you can use Blueprints. In most Flask tutorials you see throughout the web they will have a single file which contains everything including your routes, models and any other code. In Flask you have a concept called Blueprints. Static folders don’t inherit as templates do. We can also add path operations directly to the FastAPI app. So, for example, other projects could use the same APIRouter with a different authentication method. The result is that in our app, each of the path operations from the admin module will have:īut that will only affect that APIRouter in our app, not in any other code that uses it. That way, the original APIRouter will keep unmodified, so we can still share that same app/internal/admin.py file with other projects in the organization. dependencies import get_token_header router = APIRouter ( prefix = "/items", tags =, dependencies =, responses = You import it and create an "instance" the same way you would with the class FastAPI:Īpp/routers/items.py from fastapi import APIRouter, Depends, HTTPException from. You can create the path operations for that module using APIRouter. You want to have the path operations related to your users separated from the rest of the code, to keep it organized.īut it's still part of the same FastAPI application/web API (it's part of the same "Python Package"). Let's say the file dedicated to handling just users is the submodule at /app/routers/users.py. │ ├── _init_.py # makes "internal" a "Python subpackage" ![]() │ └── internal # "internal" is a "Python subpackage" │ │ └── users.py # "users" submodule, e.g. ![]() │ │ ├── items.py # "items" submodule, e.g. │ │ ├── _init_.py # makes "routers" a "Python subpackage" │ └── routers # "routers" is a "Python subpackage" ![]() │ ├── dependencies.py # "dependencies" module, e.g. │ ├── _init_.py # this file makes "app" a "Python package" Include the same router multiple times with different prefix Include an APIRouter with a custom prefix, tags, responses, and dependencies ![]() Include the APIRouters for users and items OAuth2 with Password (and hashing), Bearer with JWT tokensĪdd some custom tags, responses, and dependencies
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |