Introduction To Python Flask - Beginners Guide

Python Flask Tutorial Introduction Flask


Introduction To Python Flask  - Beginners Guide


 

This introductory Python Flask tutorial explains what is Flask, Python installation, Virtualenv, Flask Hello World example with a section on code examples, debugging and testing:

 

Web development is a greater amount of a craftsmanship than an expertise. It requires persistence and determination as well as diligence, mental fortitude and commitment to make what is important to find success. These days, understudies ought to advance as fast as could be expected.


We made this Python Flask instructional exercise for understudies to accelerate and carry out straightforward and complex web programming utilizing Python 3.


This Python Flask instructional exercise is more similar to the cup amateur instructional exercise which covers introducing Python, Virtualenv, and other vital bundles. In this instructional exercise series, we will likewise introduce Flask alongside other fundamental Flask modules. We likewise remembered a segment for code investigating, testing, and constant coordination utilizing Git activities.


You May Also Like : Linux Tip - How to Create and Manage Virtual Python Environments 



 


What is Flask

Flask is a web development framework. It is a framework with a built-in development server and debugger.

The very structure of Flask differs from other archetypes in that it allows web developers the flexibility and convenience to adapt to frequently made changes in the developer community.


What is the stock for


We use the Flask framework to create web applications in the Python programming language. Integrates with other third-party services and APIs to ensure the richness and relevance of your application under development. The basic concepts of Flask are simple and space-saving.






Prerequisites


In addition to the headings mentioned in this section, we recommend that you create a Github account. Let's move on to the steps below in the prerequisites.


Step 1: Install Python


Check if you have Python 3 installed or not. If not, download Python 3 from here and install it according to your operating system.


Step 2: Create a Python virtual environment



Create a Python virtual environment


python3 -m venv venv

Use the command below to activate the Python virtual environment.

source venv/bin/activate

Below is an example of activating and deactivating a virtual environment.

 

 

All subsequent commands in this tutorial should run in an activated virtual environment. Install the wheel pack so that we can build wheels in a virtual environment.

ad

pip install wheel



Step 3: Download and Install Flask




We need to follow the Flask download steps and install Flask by following the steps below.

Now install Flask.

pip install flask

Some of us like to work with the latest changes to the source code. We can use the command below to install with the latest changes to Flask sources.

Create a temporary directory.

mkdir tmp

Now install Flask from the Github repository. You must remain connected to the internet for the command below to work.

Look at the console outputs to check for successful installation. Now check if we have access to Flask commands.

flask --help

You can see some exceptions regarding the lack of the Flask app. However, neglect them, as we haven't created any Flask app. Our app is an instance of Flask, which is a wrapper in the Werkzeug framework and Jinja template engine.

Tool

Werkzeug is a WSGI toolkit. WSGI is just a convention for web servers forwarding web requests to web applications written in the Python programming language.

Jinja



Step 4: Install MongoDB



Install MongoDB


Follow the means beneath to introduce MongoDB. We have framed the moves toward introduce it on a Debian based Linux framework. In the event that you are utilizing an alternate  operating system access associate and introduce as per your expected  operating system.


Install gnupg to import MongoDB public GPG key.

sudo apt-get install gnupg


Now import the key with the command below.


 


Create a source list file according to your Linux distribution. We have added a list of sources according to Debian.


echo 'deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main' | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Run the update command

sudo apt-get update

Now install MongoDB using the command below.

sudo apt-get install -y mongodb-org

After the installation is successful, run MongoDB with the command below.

sudo systemctl start mongod

Check the status of MongoDB with the command below.

Now make sure mongod automatically restarts on system reboot by issuing the command below.

sudo systemctl enable mongod

Now check if you can connect to the MongoDB server using the mongo client.

mongo

In a mongo shell, try to use help and display the dbs commands.



Create a Flask app



Use the command below to install flask-appbuilder and mongoengine.

pip install flask-appbuilder pip install mongoengine pip install flask_mongoengine

Create a scaffolded app with values ​​displayed as comments in the following code snippet.

We will see an output similar to the one given below.

Your new app name: exampleApp Your engine type, SQLAlchemy or MongoEngine (SQLAlchemy, MongoEngine) [SQLAlchemy]: MongoEngine Downloaded the skeleton app, good coding!

Take a look at the layout of the project and application. Below we have shown the output of the tree command.

 

# Theme configuration for Cybord=g # these themes are located on static/appbuilder/css/themes # We can create our own and easily use them by placing them on the same dir structure to override #APP_THEME = 'bootstrap-theme.css' # default bootstrap #APP_THEME = 'cerulean.css' # cerulean #APP_THEME = 'amelia.css' # amelia theme #APP_THEME = 'cosmo.css' # cosmo theme APP_THEME = 'cyborg.css' # cyborg theme #APP_THEME = 'flatly.css' # flatly theme

To run a scaffolded application, use the command below in a terminal.

flask run

Flask Hello World


To create the first program in flaskTutorialApp, open the views.py file in the application directory and add the following code. Look for the import instructions provided in the file. Add these statements if they don't already exist.

ad

from flask_appbuilder import BaseView, expose from app import appbuilder class HelloWorld(BaseView): ''' This first view of the tutorial ''' route_base = '/hello' @expose('/') def hello(self): return 'Hello, World! from Software Testing Help' # at the end of the file appbuilder.add_view_no_menu(HelloWorld())

Save the file after adding the above source code. Go to the project root directory and use the command below to start the Flask development server.

flask run

Now go to http: // localhost: 5000 / hello / to see the output in the browser.

 

Dbms Open Source Is:

 

 

Debugging

Currently the development server is not in debug mode. Without debug mode, it's hard to find bugs in your Flask application's source code.

ad

The debugging mode in Flask produces the following results:


1. Troubleshoot mode actuates programmed reloading. This intends that subsequent to making changes to the application source code, we don't need to restart the advancement server.


2. Troubleshoot mode actuates the Python debugger. We can really look at the upsides of the factors during the special case.


3. Troubleshoot mode permits you to investigate your Flask application. We can really look at the upsides of different factors during an investigating meeting.


Stop the advancement server on the off chance that it's now running. You can utilize CTRL + C or Keyboard Break to do likewise.



Use the code below to enable debug mode and temporarily start the development server.

FLASK_ENV=development flask run

ad

Look for the debugger PIN in the console and make a note of it.

Now let's change the above HelloWorld view with the following lines of the code snippet. Note that we made a custom exception.

@expose('/') def hello(self): raise Exception('A custom exception to learn DEBUG Mode') return 'Hello, World! from Software Testing Help'


 

Likewise, investigate the control center the advancement server is running on. You will figure out that this opportunity changes to the views.py document are recognized naturally and the investigate server begins without anyone else. Presently we don't have to restart it physically.

The console will have lines as shown below. We need to note the debug PIN for later.

* Detected change in 'https://cdn.softwaretestinghelp.com/work/sth/flaskTutorialApp/app/views.py', reloading 2020-06-02 14:59:49,354:INFO:werkzeug: * Detected change in 'https://cdn.softwaretestinghelp.com/work/sth/flaskTutorialApp/app/views.py', reloading * Restarting with stat 2020-06-02 14:59:49,592:INFO:werkzeug: * Restarting with stat * Debugger is active! * Debugger PIN: 150-849-897

Presently check the stack follow in your program and go to the last line. Click it to grow its view and snap the CLI symbol to open a shell in intuitive mode.


Once opened, you will see that the program prompts you to enter your troubleshoot PIN. Enter the investigate PIN and snap OK.


As we continue on in the wake of giving the troubleshoot PIN, we can get to the intelligent shell.


We access the shell from inside the program and can really take a look at variable qualities to track down the reason for the special case and better handle the blunder. Kindly investigate one of the models displayed in the image beneath.

 

Comments

Popular posts from this blog

Exclusive Details about Mangakakalot APK Android

Learning programming kill your imagination - is that True?