Flask is a popular micro web framework that provides you with tools, libraries, and technologies for building web pages, e-commerce applications, and much more. There is a common problem for beginners when selecting what framework to learn (for backend web development). The common choices for developers are Flask and Django. I recommend Flask for a beginner, because it is easier to learn and use. Show
We could use the analogy of a compact home library (when talking about Flask), while Django would be more of a state library. The point here is that, when you intend to build apps that have minimal functionality, Flask is one of the best choices. However, because of its extensibility, organizations like Netflix, Airbnb, Uber, Lyft, Mozilla, and MIT make use of Flask as one of the technologies when developing their web applications. By the end of this tutorial, you will adequately understand the following:
Installing FlaskThe Flask web framework is built on the Python programming language, thus, it will require that we install Python. Albeit, your system might have Python installed; to check if you have Python installed, open your terminal or cmd (in Windows) and type in the command:
If you have Python already installed, you should see the version appear in your terminal when you run either of the commands. If you don’t have Python installed, we will describe the installation process next. Installing PythonThe installation process of Python is quite easy. For windows users, visit the Python official website and download the .exe file. Launch the file and follow the installation process. For Linux users, on the other hand, Python2 is mostly installed by default, but to upgrade to the latest version, go through the official documentation. Installing a virtual environmentA virtual environment is a tool that helps keep the dependencies required in different projects separate. A virtual environment is designed to allow you to work on multiple projects that require diverse dependencies. You may have a project that requires the use of SQLAlchemy in your Flask application, but you don’t want this particular dependency to be a global one in every project. A virtual environment would be ideal, as it gives you control over that. To install your virtual environment, you need Pip installed. What is PipPip is a package manager for Python packages and modules. You can follow through the step-by-step process provided by the Python official documentation to install pip and a virtual environment. Installing FlaskNow that you have Python, pip, and a virtual environment installed on your system. We can proceed to the installation of Flask itself. This is a step-by-step process on how to install Flask: Terminal and file directoryThe first step is to create the flask_website directory: Next, change directory to the flask_website directory created in the previous step: Create a virtual environment and store its tools in the “env” folder: By following the steps above, we have our virtual environment ready for our Flask app, next, we will activate it. Running a virtual environmentTo activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “env” folder as we demonstrated in the previous step.
If you have successfully activated your virtual environment, you should see the (env) word indicating that we are working in a virtual environment. With all these completed, the fun part can now begin, what you’ve been waiting for, installing Flask! Installing FlaskIn this step, you will be installing the Flask web framework in our virtual environment specifically for the project being worked on. Before you install Flask, I recommend you check your Python and Pip version just to confirm they are installed and activated in your virtual environment. You can proceed to install Flask by running the following command: Launching the workspaceFor the scope of this article, we will be using Visual Studio Code here. Now, back to your terminal after installing Flask, run the subsequent command to launch VSCode in the current directory of the web application. Now, let’s proceed to structure the web application. Structuring your web applicationThe structuring of your web application is the first thing to do before delving into the coding process, and below is a diagrammatic representation of how your Flask app should be structured: The first process in structuring your web application is to create an “app” folder, a The “app” folder structure provides us with the flexibility of defining our Flask application as a package that could be imported in any part of the app that we would require. The To start off, head back to your terminal and run the following command to generate the “requirements.txt” file:
To see the packages in use open the “requirement.txt file”. It shouldn’t be more than seven requirements. Next, you should create the “app” folder and the The Run.py fileThis file serves as a pointer to Flask, informing it about the existence of our application and to run the application. In this file, we start off by importing the app module from the app folder we created earlier, while this seems confusing, it will make sense soon. Next, we write an “if” condition to run the app when we run the Flask server, by the end of this, your
The app folderIn the previous step, you wrote the necessary code for Python to execute the app when we run the Flask server. In this step, we will completely structure the app folder, and to do that you need to create certain files and folders. We start off by creating two folders and two Python files, which are: static folder, templates folder, The Lastly, we import the Your code in the
If you recall in the previous step we imported the app module from the app file, what we were basically doing was importing the app object in the init file. Let’s proceed by examining the The views.py fileThe views file contains the routes to webpages and might also house logic for your web app. In our case, we’ll start with a function that returns a simple “Hello world’ message to our browser. To achieve this add the code below to the
From the first line of code, we imported the app object from the app itself. Next, we define the route using the @app.route decorator, and passing the URL of the web page. In this case, it’s the homepage and we use the backward slash to delineate that. Then let’s create an index() function that returns a simple hello world message. At this stage we can run our application to see what it looks like on a web browser. Running your applicationCongratulations, you’ve made it this far, you can run your application by opening your terminal with the virtual environment active and run the following command line:
By executing the command, your Flask server should be running and you can click on the link to see if your application works. If you’ve followed the process outlined here carefully, you should see hello world in your web browser. ConclusionBy now you should be able to install Flask, structure your web application professionally, and create a web page with Flask that prints hello world in your web browser. With this introduction, you can now build on it to create web applications. You can find the full codebase for this article here. Peer Review Contributions by: Adrian Murage How do you install Flask?How To Install Flask. Step 1: Install Virtual Environment. Install virtualenv on Linux. ... . Step 2: Create an Environment. Create an Environment in Linux and MacOS. ... . Step 3: Activate the Environment. Activate the Environment on Linux and MacOS. ... . Step 4: Install Flask.. Step 5: Test the Development Environment.. What is the Flask command?The flask run` command provides options to set the server listening IP address and port, SSL certificates, etc: (venv) $ flask run --help Usage: flask run [OPTIONS] Run a local development server. This server is for development purposes only.
How do I install a Flask in Windows?Install Flask. Python Version.. Dependencies. Optional dependencies. greenlet.. Virtual environments. Create an environment. Activate the environment.. Install Flask.. How do I run a Flask in command prompt?Click the + (Add New Configuration) button and select Python. Give the configuration a name such as “flask run”. For the flask run command, check “Single instance only” since you can't run the server more than once at the same time. Select Module name from the dropdown (A) then input flask .
|