![flask blueprint flask blueprint](https://i.ebayimg.com/images/g/zWoAAOSwOfxeoHaw/s-l640.jpg)
Flask blueprint code#
Use the below-given code snippet to create hello.html and save it under hello2/templates/hello2/ path. This pattern avoids conflicts between the templates of the same names. For this hello2 Blueprint, we create a new template by extending the previous one.įurthermore, notice that we have used a path semantic hello2/hello.html while calling render_template to render greeting in the template. If you have been reading our previous tutorials, then you must have noticed that the template is of the same name as we created for rending the HelloWorld view. Now let’s create the template hello.html. In this function, we assign a value to a variable called greeting and then pass it to the render_template method. Then we define a route, using the decorator and a controller with the name hello2_view. Then we create a Blueprint object by giving the names of the template and static directories. We import the Blueprint class and render_template method. Return render_template("hello2/hello.html", greeting=greeting) from flask import Blueprint, render_template Open hello2/views.py and enter the below-given code snippet. Now let’s define a simple view inside views.py. Use the tree command to create a similar output. The directory structure for the Blueprint should look as shown below. We create a separate directory for our Blueprint with templates and static directories to render Flask views and serve static resources, respectively. We want our hello2 Blueprint to have its views that we will import in its _init_.py. Touch "$blueprint_name/templates/$blueprint_name/hello.html" Mkdir -p "$blueprint_name/templates/$blueprint_name" Use the below-given script to create the directory structure after activating your virtual environment by using the source venv/bin/activate after going to the application directory. Let’s start creating a Flask Blueprint with the name of hello2. Create Flask Blueprint Files And Directories However, that is likely to create confusion and is not a good practice. You can create multiple Blueprints from the same set of resources. It means that a Flask Blueprint can have its views, forms, models, static files, and templates. Let’s create a HelloWorld blueprint.Īll components, resources, and features of a blueprint are kept and organized separately from the other source code of the Flask application. We can think of Blueprint as a module too. With the above background given on Flask Blueprint, we can move ahead and design our first Blueprint.
![flask blueprint flask blueprint](https://www.manejandodatos.es/wp-content/uploads/2014/09/flask.png)
from flask import Flaskįlask Blueprint lets us keep related features together and helps in better development practices. The instance of the class Flask is the Flask Application, which is the instance of WSGI application. If you notice the code written in the _init_.py file then you will notice that an instance of Flask is created, and the variable is named as app. Modular Flask Application Example With Flask Blueprintįlask App is another name for a Flask Application.Register A Flask Blueprint With Flask App.Create Flask Blueprint Files And Directories.