Flask is a web framework, which is a Python module that allows you to easily develop web applications. It has a small and easily expandable core: it is a microframework that does not include an ORM (Object Relational Manager) or similar functionality.
It has lots of cool features like URL routing, and a template engine. It is a WSGI web application framework.let's start your first application with flask
- Create a directory with the name flask
> mkdir flask
> cd flask > py -m venv flask
> flask\Scripts\activate
> pip install flask
Your all steps should be like this -
> mkdir firstproject
Inside the firstproject folder create a file with name views.py
- Program - 1: Print hello world in the flask, So let's start to print hello world in a flask, here we will create a web application with the following code:
from flask import Flask app = Flask(__name__) @app.route('/') def helloworld(): return "Hello, World!"
So what did that code do?
- First, we imported the Flask class.
- __name__ is a shortcut for this that is appropriate for most cases. This is needed so that Flask knows where to look for resources such as templates and static files.
- We then use the route() decorator to tell Flask what URL should trigger our function.
- The function returns the message we want to display in the user’s browser. The default content type is HTML, so HTML in the string will be rendered by the browser.
Save the views.py file and run your code
> flask --app views run
Once the server gets started it will be like this -
- Open the link and get your first experience with your flask application
http://127.0.0.1:5000
Comments
Post a Comment