flask boiler plate
To create a Flask boilerplate, follow these steps:
Install Flask: Begin by installing Flask, a micro web framework for Python, using the pip package manager. Run the command
pip install flask
in your terminal to install Flask.Set up the project structure: Create a new directory for your Flask project. Inside this directory, create a file named
app.py
, which will serve as the entry point for your Flask application. Additionally, create a folder namedtemplates
to store your HTML templates.Import Flask: In the
app.py
file, import the Flask module by adding the following line at the top of the file:from flask import Flask, render_template
Create a Flask instance: Create an instance of the Flask class by adding the following line below the import statement:
app = Flask(__name__)
. The__name__
parameter is a special Python variable that represents the name of the current module.Define routes: Define the routes for your application by adding functions to handle specific URLs. For example, to handle the root URL, add the following function below the Flask instance creation:
@app.route('/')
def index():
return "Hello, Flask!"
- Render templates: To render HTML templates, create a
templates
folder and add an HTML file inside it. For example, create a file namedindex.html
inside thetemplates
folder. In theindex()
function, use therender_template()
function to render the HTML template:
@app.route('/')
def index():
return render_template('index.html')
- Run the application: At the end of the
app.py
file, add the following lines to run the Flask application:
if __name__ == '__main__':
app.run(debug=True)
Start the server: In your terminal, navigate to the project directory and run the command
python app.py
to start the Flask development server. You should see an output indicating that the server is running.Test the application: Open a web browser and visit
http://localhost:5000
to see your Flask application in action. Theindex()
function will be called, and the returned value or rendered template will be displayed in the browser.
This completes the basic setup of a Flask boilerplate. You can now start building your Flask application by adding more routes, views, and functionality as needed.