In this Flask CRUD Application lesson we are going to talk about Flask CRUD Application Project Structure, we want to create our first project in flask, i have already installed flask, now open your favorite IDE, in my case iam using Pycharm IDE.
Installation
For this lesson first you need to install flask, and after that you need to install flask-sqlalchemy.
1 |
pip install Flask |
1 |
pip install Flask-SQLAlchemy |
After installation create two folders, the first one is static where we want to add our static files like images, javascript and other files, the second one is templates folder where we want to add our html templates, for the bootstrap templates we are going to use Bootstrap CDN links, also create a Python file at name of App.py.
This is our Flask Project Structure for CRUD Application.
Open your App.py file and create your first Flask application using this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from flask import Flask #create the object of Flask app = Flask(__name__) #creating our routes @app.route('/') def Index(): return "<h1>Flask CRUD Application " \ "- geekscoders.com</h1>" #run flask app if __name__ == "__main__": app.run(debug=True) |
First of all Flask Applications must create an application object, The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI). you can create the application object like this.
1 |
app = Flask(__name__) |
The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s __name__ variable is the correct value.
Also for every Flask Application we need to define routes, you can use app.route decorator for creating routes in Flask, which registers the decorated function as a route. as we have already done in our above complete code like this, In this code we have registered the function index() as the handler for the applications’s root URL. functions like Index() are called view functions. a response returned by a view function can be a simple string with HTML content, but it can also take more complex forms.
1 2 3 4 |
@app.route('/') def Index(): return "<h1>Flask CRUD Application " \ "- geekscoders.com</h1>" |
At the end we need to run our web application using development server.
1 2 |
if __name__ == "__main__": app.run(debug=True) |
The __name__ == ‘__main__‘ is used here to ensure that the development web server is started only when the script is executed directly, and also you can see that we have made debugging mode True, but when you are going to publish your web application , you need to make false, for development purposes you can keep it true.
Now run your application, and go to http://localhost:5000/. this will be the result.
OK now let’s add html file to our templates folder, iam going to name it index.html.
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flask CRUD Applicaiton</title> </head> <body> <h1>Hello Template - geekscoders.com</h1> </body> </html> |
Now open your App.py file, instead of returning just simple string we are going to return a template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from flask import Flask, render_template #create the object of Flask app = Flask(__name__) #creating our routes @app.route('/') def Index(): return render_template("index.html") #run flask app if __name__ == "__main__": app.run(debug=True) |
Run your application and this is the result.