BackendGuy
Flask for Dummies

[episode 4] Creating an online platform in flask for dummies

In our previous post here we had to install all the dependencies which we needed such as the flask package and the flask-sqlalchemy for the database, now up next we’re going to start coding the application, the only thing i’ll want you to do is to create an additional folder inside the venv folder,name that folder app and then inside the app folder, you create two folders called static, templates, now the reason we are actually creating these folders is because the flask application we’re building will not serve our stylesheets manually, or serve our images and javascript files manually, it has to look into a folder called static and then to render our views it has to look into a folder called templates.

Visit the episode one of this tutorial

Visit the episode two of this tutorial

Now let me buttress more on this, what i mean is that the static folder will contain our stylesheets and javascript files, while the template folder will contain our basic html files which we would create to render the designs, so what next? lets proceed to coding! i supposed you must have created those files.

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.

 

flask loading in cmd
Fig 1.0: flask loading in cmd

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.

flask display
flask display

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.

BackendGuy

Add comment

Your Header Sidebar area is currently empty. Hurry up and add some widgets.