First Flask App
After the successful installation and configuration of Flask, let’s try to build our very first app using flask.
The code for our app is given below:
from flask import Flask app = Flask(__name__) @app.route('/') def home_fun(): return "hi, this is our very first flask webpage"; if __name__ =='__main__': app.run(debug = True)
The above code is saved with a .py extension.
Now let's run the above code using the following steps.
- Open the cmd and go to the directory where you created your project folder. I created my project folder in the documents as shown below:
- Now ,to enter the virtual environment type, enter the command py -m venv env,
and hit the enter.
- Then type another command env\scripts\activate and hit enter now, you are in the virtual environment, and it can be cross-checked by looking for (env) in the cmd left side as we can see it in the below screenshot:
- After that, type the following commands to run your code and hit enter
set FLASK_APP=1flaskcode.py
Here (1flaskcode.py) is your file name.
- The last command to run your code is flask run. After entering this command, press enter, then the server gets started. It will look like something as shown below.
Now copy the highlighted URL address and paste it to any web browser as shown in the below screenshot: