Code 1.0 : Basic welcome template code in flask
Recall here that we installed flask, so we need to use it here, now in python to use an existing package you need to import that package, and that is why we would be dealing with lots of import rules here. so we are starting with a basic welcome template code in flask.
# Import the flask package to be used in the application from flask import Flask # initialize the flask application as an App app = Flask(__name__) #create a route to be rendered in the webPage @app.route('/home') #define a function that would trigger the home url #to be redirected to the rendered view def homepage(): return "Hello flask" #run the application main Loop for event to be #carried out explictly if __name__ == '__main__': app.run(debug=True)
Now if you read the code it’s pretty understandable because of the comments, the # symbol is used for commenting in python, now to run this code go to your command prompt and follow the steps below.
c:\users\N\desktop\flaskonlineplatform\venv\scripts> activate <venv>c:\users\N\desktop\flaskonlineplatform\venv\scripts> cd.. <venv>c:\users\N\desktop\flaskonlineplatform\venv> cd app <venv>c:\users\N\desktop\flaskonlineplatform\venv\app> python app.py
Then wait for the environment to load, after loading it would show you that the flask app has loaded, it would load the debugger pin also (this is because we specified debug=True) and then show you the url to navigate to get your flask app running.
Now after you have done this go to any browser of your choice (i personally use chrome) and then run the url 127.0.0.1:5000, it should display the result of our app.
You have suceeded in creating your first Flask app! hurray!! , Now calm down that was just the basics, the next post would introduce us to something better.