Welcome to the second episode of creating an online platform in flask for dummies, actually i had already talked about some very important stuffs here so click that link and read it before coming to read this particular tutorial, because i really would not love you to be left behind, now once again my name is Jim and am popularly known as Metro (i love metro boomin!) and am a backend developer with interests in PHP, NodeJS and Python, i actually reside currently in Africa. Now thats about me, what about flask?
Even though i had already made a post on introduction to flask i would want to buttress more on it even though i really advice you to read that post.
Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.
Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons. Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins. In the case of Flask, its dependencies are:
What is WSGI?
WSGI is basically a protocol defined so that Python application can communicate with a web-server and thus be used as web-application outside of CGI (common gateway interface).
What are template engines?
Have you ever built a website? Did you face the problem that to keep the style of the website consistent, you have had to write multiple times the same text? Did you ever tried to change the style of such website?
If your website contains only few pages, changing its style will take you some time but is doable. However, if you have a lot of pages (for example the list of items you sell in your store), this task become overwhelming.
Using templates you are able to set a basic layout for your pages and mention which element will change. This way you can define your header once and keep it consistent over all the pages of your website, and if you need to change your header, you will only have to update it in one place.
Using a template engine will save you a lot of time when creating your application but also when updating and maintaining it.
How to install flask on your computer
Python 2.6 or higher is usually required for installation of Flask. Although Flask and its dependencies work well with Python 3 (Python 3.3 onwards), many Flask extensions do not support it properly. Hence, it is recommended that Flask should be installed on Python 2.7.
so firstly, you need to download python if you have not downloaded python before, so to download python kindly visit this page it should take you to the official python webite where you will be able to download python for use, after downloading python, install it following instructions that would be given to you as you do so.
Now we need a virtual environment to run our flask application, we have gotten python, but we need this venv(virtual environment), so how do we do this, kindly follow the examples, open up your command prompt, for this tutorials am using a windows operating system, so i’ll just type cmd in my search box, and click command prompt, and then my command prompt opens. check the diagram below.
Now we have opened up our command prompt, the next thing to do is to navigate to the folder where our python package which we downloaded is stored, and then from there we can start our virtual environment.
now lets say your python package was stored in the c:/python27 folder kindly navigate to that folder on your command prompt, and navigate again to the scripts folder and then run the virtual environment, see my examples below.
Now we have navigated to the folder where our python was downloaded, the next thing is to run our virtual environment and wait for it to initialize. to initialize it use the code below
c:\python27\scripts> virtualenv venv
Now we are really progressing, by now i believe you have not found any difficulty in running this, this is as easy as you ever imagined right? , now the goodnews is that we have successfully set up our flask virtual environment, now its time to start building our platform, but wait!, we need to install flask and also create folders that would house our projects, besides that we also need to install the database file which we would be using, for this tutorials we would be using flask-sqlalchemy.
so whats next? lets create our folder for the project, choose a directory suitable for you, personally, i choose my desktop.
Creating folders and installing files
Now to do this we need to go back to our parent directory which is our local disk and then navigate to the desktop where we would create our folders and then set up real time projects. type the codes below in your command prompt , it should take you to the local directory then navigate to your preferred location and create the required folders.
cd/ c:\> cd users\N\desktop c:\> mkdir flaskonlineplatform c:\> cd flaskonlineplatform c:\users\N\desktop\flaskonlineplatform> c:\python27\scripts\virtualenv venv
Now what did we do here, we navigated to our desktop using command prompt, and then created a folder named flaskonlineplatform using the mkdir/ cd command and then finally set up a virtual environment inside the folder that would help us to run our scripts,now if you have done all this check your desktop you should see the new folder flaskonlineplatform, and click on it, another folder would be there too , named venv.
Now we should have a folder created on our desktop named flaskonlineplatform, and inside it, it should have another folder named venv.
Now click on that folder, another folder named venv would be inside it, this venv folder contain several other configuration files for the application we would be creating, now i hope we’ve been able to create and set up our basic working environment, now next up would be how to start coding and finishing up our setting files.
Check the next episode of the flask for dummies.