{"id":139588,"date":"2020-11-04T12:46:59","date_gmt":"2020-11-04T12:46:59","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=139588"},"modified":"2025-04-01T08:34:40","modified_gmt":"2025-04-01T08:34:40","slug":"python-flask-tutorial","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/python-flask-tutorial\/","title":{"rendered":"Python Flask Tutorial &#8211; Introduction To Flask For Beginners"},"content":{"rendered":"<p><strong>This introductory Python Flask tutorial explains what is Flask, installation of Python, Virtualenv, Flask Hello World example with a section on code examples, debugging, and testing:<\/strong><\/p>\n<p>Website development is more of an art than a skill. It requires patience and diligence, along with perseverance, courage, and dedication to create what&#8217;s necessary for it to be a real success. These days, it is essential for learners to get to speed as soon as possible.<\/p>\n<p>We have created this Python Flask tutorial for the students to get up to speed and implement simple as well as complex web programming using Python 3.<\/p>\n<p><strong><em> <\/em><\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Flask-Python-Tutorial-1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-139672\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Flask-Python-Tutorial-1.png\" alt=\"Flask Python Tutorial\" width=\"700\" height=\"394\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Flask-Python-Tutorial-1.png 700w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Flask-Python-Tutorial-1-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>This Python Flask tutorial is more like a flask beginner tutorial, which will cover the installation of Python, Virtualenv, and other essential packages. In this series of tutorials, we will also install Flask along with the other necessary Flask plugins. We have also included a section on code debugging, testing, and continuous integration using Git Actions.<\/p>\n<h2>List Of Tutorials In This Flask Series<\/h2>\n<p><span style=\"color: #ff6600;\"><strong>Tutorial #1:<\/strong><\/span> <a href=\"https:\/\/www.softwaretestinghelp.com\/python-flask-tutorial\/\">Python Flask Tutorial &#8211; Introduction To Flask For Beginners<\/a><br \/>\n<strong><span style=\"color: #ff6600;\">Tutorial #2:<\/span><\/strong> <a href=\"https:\/\/www.softwaretestinghelp.com\/flask-template-form-view-and-redirect\/\">Flask Template, Form, View, And Redirect With Examples<\/a><br \/>\n<span style=\"color: #ff6600;\"><strong>Tutorial #3:<\/strong><\/span> <a href=\"https:\/\/www.softwaretestinghelp.com\/how-to-use-flask-with-a-database\/\">Flask Database Handling &#8211; How To Use Flask With A Database<\/a><br \/>\n<strong><span style=\"color: #ff6600;\">Tutorial #4:<\/span><\/strong> <a href=\"https:\/\/www.softwaretestinghelp.com\/flask-app-with-blueprint-and-bootstrap\/\">Flask App And Flask Project Layout With Blueprint &amp; Bootstrap<\/a><br \/>\n<strong><span style=\"color: #ff6600;\">Tutorial #5:<\/span> <\/strong><a href=\"https:\/\/www.softwaretestinghelp.com\/flask-design-patterns-for-web-applications\/\">Flask Design Patterns And Best Practices For Web Applications<\/a><br \/>\n<strong><span style=\"color: #ff6600;\">Tutorial #6:<\/span><\/strong> <a href=\"https:\/\/www.softwaretestinghelp.com\/flask-api-tutorial-with-examples\/\">Flask API Tutorial With Example | Extending Flask With APIs<\/a><br \/>\n<strong><span style=\"color: #ff6600;\">Tutorial #7:<\/span><\/strong> <a href=\"https:\/\/www.softwaretestinghelp.com\/django-vs-flask-vs-node\/\">Django Vs Flask Vs Node: Which Framework To Select<\/a><br \/>\n<strong><span style=\"color: #ff6600;\">Tutorial #8:<\/span> <\/strong><a href=\"https:\/\/www.softwaretestinghelp.com\/flask-interview-questions-with-answers\/\">Top 31 Popular Python Flask Interview Questions With Answers<\/a><\/p>\n<hr \/>\n<h2>What Is Flask<\/h2>\n<p><strong>Flask is a web development framework.<\/strong> It is a framework with a built-in development server and a debugger.<\/p>\n<p>Flask framework in itself is different from the other archetypes as it allows web developers to be flexible and to comfortably accommodate the frequently released changes in the software development community.<\/p>\n<h3>What Is Flask Used For<\/h3>\n<p>We use the Flask framework for developing Web Applications in Python programming language. It integrates with other third-party services and APIs to bring richness and meaning to the application under development. Flask&#8217;s core concepts are simple, and it has a tiny footprint.<\/p>\n<p>Let&#8217;s start with this Python Flask tutorial to understand web development with the help of a Flask GitHub repository. However, before proceeding, please clone the project from Github for easy follow up on the discussed code examples.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Other than the mentioned headings in this section, we recommend creating a Github Account. Let&#8217;s proceed with the below-mentioned steps in the prerequisites.<\/p>\n<p><strong>Step 1: Install Python<\/strong><\/p>\n<p>Check whether you have installed Python 3 or not. If not, then download Python 3 from\u00a0<a href=\"https:\/\/www.python.org\" target=\"_blank\" rel=\"noopener nofollow\">here<\/a> and install it as per your Operating System.<\/p>\n<p><strong>Step 2: Create a Python Virtual Environment<\/strong><\/p>\n<p>Create a virtual environment using the below command.<\/p>\n<pre>python3 -m venv venv<\/pre>\n<p><strong>Use the below command to activate the Python virtual environment.<\/strong><\/p>\n<pre>source venv\/bin\/activate<\/pre>\n<p>We have given an example of activation and deactivation of the virtual environment below.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/activation-and-deactivation.png\"><img decoding=\"async\" class=\"alignnone wp-image-139592 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/activation-and-deactivation.png\" alt=\"activation and deactivation of virtual environment\" width=\"591\" height=\"88\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/activation-and-deactivation.png 591w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/activation-and-deactivation-300x45.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><\/p>\n<p>All the subsequent commands in this tutorial should run in an activated virtual environment. Install the wheel package so that we can build wheels inside the virtual environment.<\/p>\n<pre>pip install wheel<\/pre>\n<p><strong>Step 3: Flask Download And Insall<\/strong><\/p>\n<p>We need to perform the Flask download steps and install Flask using the below-mentioned steps.<\/p>\n<p>Now install Flask.<\/p>\n<pre>pip install flask<\/pre>\n<p>Some of us like to work along with the latest source code changes. We can use the below-given command to install with the latest changes to the sources of Flask.<\/p>\n<p>Make a temporary directory.<\/p>\n<pre>mkdir tmp<\/pre>\n<p>Now install Flask from the Github repository. You need to remain connected to the internet for the below command to work.<\/p>\n<pre>pip3 install -e git+git@github.com:pallets\/flask.git#egg=flask<\/pre>\n<p>Look at the console outputs to check the successful installation. Now check if we can access Flask commands.<\/p>\n<pre>flask --help<\/pre>\n<p>You might see some exceptions about the absence of a Flask application. However, neglect those as we have not created any Flask app. Our app is an instance of Flask, which is a wrapper on the Werkzeug web framework and Jinja templating engine.<\/p>\n<p><strong>Werkzeug<\/strong><\/p>\n<p>Werkzeug is a WSGI toolkit. WSGI is only a calling convention for web servers to forward <strong>web requests<\/strong> to web applications written in Python programming language.<\/p>\n<p><strong>Jinja<\/strong><\/p>\n<p>Templating is an essential skillset of web developers. Jinja is a fully-featured and popular templating engine for Python. It is quite an expressive language and provides a robust set of tools to template authors.<\/p>\n<p><strong>Step 4: Install MongoDB<\/strong><\/p>\n<p>Follow the below-mentioned steps to install MongoDB. We have outlined the steps to install it in a Debian based Linux. If you are using another operating system, then access the\u00a0<a href=\"https:\/\/www.mongodb.com\/try\/download\/community\" target=\"_blank\" rel=\"noopener nofollow\">link<\/a> and install as per the intended operating system.<\/p>\n<p><strong>Install gnupg for importing MongoDB public GPG key.<\/strong><\/p>\n<pre>sudo apt-get install gnupg<\/pre>\n<p><strong>Now import the key using the command below.<\/strong><\/p>\n<pre>wget -qO - https:\/\/www.mongodb.org\/static\/pgp\/server-4.2.asc | sudo apt-key add -<\/pre>\n<p><strong>Create the sources list file as per your Linux distribution. We have added the list of sources as per Debian.<\/strong><\/p>\n<pre>echo \"deb http:\/\/repo.mongodb.org\/apt\/debian buster\/mongodb-org\/4.2 main\"\r\n | sudo tee \/etc\/apt\/sources.list.d\/mongodb-org-4.2.list<\/pre>\n<p><strong>Run update command<\/strong><\/p>\n<pre>sudo apt-get update<\/pre>\n<p><strong>Now install MongoDB, using the below command.<\/strong><\/p>\n<pre>sudo apt-get install -y mongodb-org<\/pre>\n<p><strong>Once the installation is successful, start MongoDB using the below command.<\/strong><\/p>\n<pre>sudo systemctl start mongod<\/pre>\n<p><strong>Check the status of MongoDB, using the command below.<\/strong><\/p>\n<pre>sudo systemctl status mongod<\/pre>\n<p><strong>Now ensure that mongod automatically starts at system reboot by issuing the below-shown command.<\/strong><\/p>\n<pre>sudo systemctl enable mongod<\/pre>\n<p><strong>Now check if you can connect to the MongoDB server using mongo client.<\/strong><\/p>\n<pre>mongo<\/pre>\n<p>In the mongo shell, try using the help and show dbs commands.<\/p>\n<h3>Create A Flask App<\/h3>\n<p><strong>Use the command below to install flask-appbuilder and mongoengine.<\/strong><\/p>\n<pre>pip install flask-appbuilder\r\npip install mongoengine\r\npip install flask_mongoengine<\/pre>\n<p><strong>Create a skeleton app with the values shown as comments in the below code snippet.<\/strong><\/p>\n<pre>flask fab create-app\r\n# Give the following values in the command line questionnaire\r\n# Application Name: flaskTutorialApp\r\n# EngineType : MongoEngine<\/pre>\n<p>We will see the output similar to the one given below.<\/p>\n<pre>Your new app name: exampleApp\r\nYour engine type, SQLAlchemy or MongoEngine (SQLAlchemy, MongoEngine) \r\n[SQLAlchemy]: MongoEngine\r\nDownloaded the skeleton app, good coding!<\/pre>\n<p><strong>Have a look at the layout of the project and the app. We have shown the output of the tree command below.<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/output-of-the-tree-command.png\"><img decoding=\"async\" class=\"alignnone wp-image-145021 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/output-of-the-tree-command.png\" alt=\"output of the tree command\" width=\"179\" height=\"247\" \/><\/a><\/p>\n<p>Let&#8217;s have a look at the Flask config file. It is a default config generated as the result of the last command. Uncomment <strong>Cyborg<\/strong> theme, as shown below.<\/p>\n<pre># Theme configuration for Cybord=g\r\n# these themes are located on static\/appbuilder\/css\/themes\r\n# We can create our own and easily use them by placing them on the same dir structure to override\r\n#APP_THEME = \"bootstrap-theme.css\" # default bootstrap\r\n#APP_THEME = \"cerulean.css\" # cerulean\r\n#APP_THEME = \"amelia.css\" # amelia theme\r\n#APP_THEME = \"cosmo.css\" # cosmo theme\r\nAPP_THEME = \"cyborg.css\" # cyborg theme\r\n#APP_THEME = \"flatly.css\" # flatly theme<\/pre>\n<p>To run the skeleton app, use the below-given command on the terminal.<\/p>\n<pre>flask run<\/pre>\n<h3>Flask Hello World<\/h3>\n<p>To create your first program in the flaskTutorialApp, open file views.py under the app directory and add the following code. Look for import statements given in the file. Add these statements if not already present.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">from flask_appbuilder import BaseView, expose\r\n\r\nfrom app import appbuilder\r\n\r\nclass HelloWorld(BaseView):\r\n    &quot;&quot;&quot; This first view of the tutorial &quot;&quot;&quot;\r\n    route_base = &quot;\/hello&quot;   \r\n\r\n @expose(&quot;\/&quot;)\r\n    def hello(self):\r\n        return &quot;Hello, World! from Software Testing Help&quot;\r\n\r\n# at the end of the file\r\nappbuilder.add_view_no_menu(HelloWorld())<\/pre>\n<p>Save the file after adding the above source code. Go to the project&#8217;s root directory and use the below command to run the Flask&#8217;s development server.<\/p>\n<pre>flask run<\/pre>\n<p>Now navigate to http:\/\/localhost:5000\/hello\/ to see the output in the browser.<\/p>\n<h3>Debugging<\/h3>\n<p>Currently, the development server is not running in the Debug mode. Without debugging mode, it is difficult to find the errors in the source code of the Flask Application.<\/p>\n<p><strong>Debug Mode in Flask results in the following:<\/strong><\/p>\n<ol>\n<li>Debug mode activates Automatic reloader. It means that we need not restart the development server after we make changes in the source code of the application.<\/li>\n<li>Debug mode activates the Python debugger. We can inspect the values of variables during the Exception.<\/li>\n<li>Debug mode enables Flask application debugging. We can check the values of various variables in debugging sessions.<\/li>\n<\/ol>\n<p>Stop the development server if it is already running. You can use CTRL + C or Keyboard interrupt to do the same.<\/p>\n<p><strong>Use the following code to enable the debug mode and run the development server temporarily.<\/strong><\/p>\n<pre>FLASK_ENV=development flask run<\/pre>\n<p>Search the console for Debugger PIN and make a note of it.<\/p>\n<p>Now let&#8217;s change the above written HelloWorld view with the following lines of the code snippet. Notice that we have introduced a custom exception.<\/p>\n<pre>@expose(\"\/\")\r\ndef hello(self):\r\nraise Exception(\"A custom exception to learn DEBUG Mode\")\r\nreturn \"Hello, World! from Software Testing Help\"<\/pre>\n<p>Navigate to http:\/\/localhost:5000\/hello\/, and you will see that the application has raised an exception. The browser displays the stack trace, as shown below.<\/p>\n<figure id=\"attachment_145030\" aria-describedby=\"caption-attachment-145030\" style=\"width: 540px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/exception-1.png\"><img decoding=\"async\" class=\"wp-image-145030 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/exception-1.png\" alt=\"Flask Debug View in the Browser\" width=\"550\" height=\"353\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/exception-1.png 550w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/exception-1-300x193.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-145030\" class=\"wp-caption-text\">Flask Debug View in the Browser<\/figcaption><\/figure>\n<p>Furthermore, look at the console where the development server is running. You will find that this time, the changes in the views.py are automatically detected, and the debug server restarts by itself. Now we need not manually restart it.<\/p>\n<p><strong>The console will have lines, as shown below. We need to note down the Debug PIN for later.<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">* Detected change in '\/work\/sth\/flaskTutorialApp\/app\/views.py', reloading\r\n2020-06-02 14:59:49,354:INFO:werkzeug: * Detected change in\r\n '\/work\/sth\/flaskTutorialApp\/app\/views.py', reloading\r\n * Restarting with stat\r\n2020-06-02 14:59:49,592:INFO:werkzeug: * Restarting with stat\r\n * Debugger is active!\r\n * Debugger PIN: 150-849-897\r\n<\/pre>\n<p>Now check the stack trace in the browser and go to the last line. Click on it to expand its view and click on the CLI icon to open the shell in interactive mode.<\/p>\n<figure id=\"attachment_139594\" aria-describedby=\"caption-attachment-139594\" style=\"width: 598px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Debug-Mode-Interactive-Shell-Icon.png\"><img decoding=\"async\" class=\"wp-image-139594 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Debug-Mode-Interactive-Shell-Icon.png\" alt=\"Debug Mode Interactive Shell Icon\" width=\"608\" height=\"348\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Debug-Mode-Interactive-Shell-Icon.png 608w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Debug-Mode-Interactive-Shell-Icon-300x172.png 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/a><figcaption id=\"caption-attachment-139594\" class=\"wp-caption-text\">Debug Mode Interactive Shell Icon<\/figcaption><\/figure>\n<p>Once you open it, You will see that the browser will show a prompt for Debug PIN. Give the Debug PIN and click on OK.<\/p>\n<figure id=\"attachment_145031\" aria-describedby=\"caption-attachment-145031\" style=\"width: 396px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/Debug-Mode-Debug-PIN.png\"><img decoding=\"async\" class=\"wp-image-145031 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/Debug-Mode-Debug-PIN.png\" alt=\"Debug Mode Debug PIN\" width=\"406\" height=\"190\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/Debug-Mode-Debug-PIN.png 406w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/Debug-Mode-Debug-PIN-300x140.png 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-145031\" class=\"wp-caption-text\">Debug Mode Debug PIN<\/figcaption><\/figure>\n<p>Once we proceed ahead after giving the Debug PIN, we can access the interactive shell.<\/p>\n<p>We access the shell from within the browser and can inspect the values of variables to find the cause of the Exception and handle the error in a better way. Please look at one of the examples shown in the image below.<\/p>\n<figure id=\"attachment_139596\" aria-describedby=\"caption-attachment-139596\" style=\"width: 607px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Variables-in-Debug-Mode.png\"><img decoding=\"async\" class=\"wp-image-139596 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Variables-in-Debug-Mode.png\" alt=\"Variables in Debug Mode\" width=\"617\" height=\"302\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Variables-in-Debug-Mode.png 617w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Variables-in-Debug-Mode-300x147.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/a><figcaption id=\"caption-attachment-139596\" class=\"wp-caption-text\">Variables in Debug Mode<\/figcaption><\/figure>\n<p>Now change the code in view.py, as shown below. Note that we have commented on the line that had the raised Exception.<\/p>\n<pre>@expose(\"\/\")\r\ndef hello(self):\r\n      # raise Exception(\"A custom exception to learn DEBUG Mode\")\r\n      return \"Hello, World! from Software Testing Help\"<\/pre>\n<h3>Testing The Flask Application<\/h3>\n<p>Now let&#8217;s write our first test for the Flask application that we are developing. First, <a href=\"https:\/\/www.softwaretestinghelp.com\/pytest-tutorial\/\">install the PyTest<\/a>. PyTest is a testing framework. It helps us to write better code.<\/p>\n<p>Moreover, just because we can write unit tests while developing our applications, it is possible to follow the TDD approach. TDD stands for Test-Driven Development. In our subsequent tutorials of this series, we shall always write tests first and develop our views or models.<\/p>\n<p><strong>Install PyTest<\/strong><\/p>\n<pre>pip install pytest<\/pre>\n<p>Now create a directory called tests inside the app directory and in that create a file called test_hello.py. Let&#8217;s write our first unit test to test our view.<\/p>\n<p><strong>Copy the following code snippet and paste it into test_hello.py.<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">#!\/usr\/bin\/env python\r\nfrom app import appbuilder\r\nimport pytest\r\n\r\n@pytest.fixture\r\ndef client():\r\n    &quot;&quot;&quot; A pytest fixture for test client &quot;&quot;&quot;\r\n    appbuilder.app.config&#x5B;&quot;TESTING&quot;] = True\r\n    with appbuilder.app.test_client() as client:\r\n        yield client\r\n\r\ndef test_hello(client):\r\n    &quot;&quot;&quot; A test method to test view hello &quot;&quot;&quot;\r\n    resp = client.get(&quot;\/hello&quot;, follow_redirects=True)\r\n    assert 200 == resp.status_code<\/pre>\n<p>Use the below pytest command to run the tests. PyTest automatically collects the Tests and displays the results on the standard output.<\/p>\n<figure id=\"attachment_145032\" aria-describedby=\"caption-attachment-145032\" style=\"width: 613px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/PyTest-automatically.png\"><img decoding=\"async\" class=\"wp-image-145032 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/PyTest-automatically.png\" alt=\"PyTest results on Command Line\" width=\"623\" height=\"108\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/PyTest-automatically.png 623w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/PyTest-automatically-300x52.png 300w\" sizes=\"(max-width: 623px) 100vw, 623px\" \/><\/a><figcaption id=\"caption-attachment-145032\" class=\"wp-caption-text\">PyTest results on Command Line<\/figcaption><\/figure>\n<h3>Create A GitHub Workflow<\/h3>\n<p>We use Git Actions to create a CI\/CD workflow for our sample application. Follow the below-mentioned steps for your project.<\/p>\n<p><strong>Step 1: Navigate to the repository page on GitHub. Click on Git Actions.<\/strong><\/p>\n<figure id=\"attachment_139597\" aria-describedby=\"caption-attachment-139597\" style=\"width: 654px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_1.png\"><img decoding=\"async\" class=\"wp-image-139597 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_1.png\" alt=\"Click on Git Actions\" width=\"664\" height=\"140\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_1.png 664w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_1-300x63.png 300w\" sizes=\"(max-width: 664px) 100vw, 664px\" \/><\/a><figcaption id=\"caption-attachment-139597\" class=\"wp-caption-text\">Click on Git Actions<\/figcaption><\/figure>\n<p><strong>Step 2: Scroll down on the page and find an existing workflow template for a Python package.<\/strong><\/p>\n<figure id=\"attachment_145033\" aria-describedby=\"caption-attachment-145033\" style=\"width: 640px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/a-Python-package.png\"><img decoding=\"async\" class=\"wp-image-145033 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/a-Python-package.png\" alt=\"Click on Python Package Git Workflow\" width=\"650\" height=\"314\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/a-Python-package.png 650w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/10\/a-Python-package-300x145.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-145033\" class=\"wp-caption-text\">Click on Python Package Git Workflow<\/figcaption><\/figure>\n<p><strong>Step 3: Setup the Python package Workflow.<\/strong><\/p>\n<figure id=\"attachment_139600\" aria-describedby=\"caption-attachment-139600\" style=\"width: 608px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Setup-Python-package-Workflow.png\"><img decoding=\"async\" class=\"wp-image-139600 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Setup-Python-package-Workflow.png\" alt=\"Setup Python package Workflow\" width=\"618\" height=\"408\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Setup-Python-package-Workflow.png 618w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Setup-Python-package-Workflow-300x198.png 300w\" sizes=\"(max-width: 618px) 100vw, 618px\" \/><\/a><figcaption id=\"caption-attachment-139600\" class=\"wp-caption-text\">Setup Python package Workflow<\/figcaption><\/figure>\n<p><strong>Step 4: Once the python-package.yml workflow configuration opens, update it based on the given yaml additional tag values.<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">name: flaskTutorialApp\r\njobs:\r\n build:\r\n  runs-on: ubuntu-latest\r\n  strategy: \r\n  matrix:\r\n   python-version: &#x5B;3.7, 3.8]\r\n   mongodb-version: &#x5B;4.2]  \r\n\r\n  steps:\r\n   - name: Start MongoDB\r\n     uses: supercharge\/mongodb-github-action@1.3.0\r\n     with:\r\n      mongodb-version: ${{ matrix.mongodb-version }}\r\n   # other values<\/pre>\n<p>We want to test our Flask application on the latest Ubuntu Linux distribution. In addition to the OS, we want to run our tests only using Python 3.7 and Python 3.8.<\/p>\n<p><strong>Step 5: Commit the python-package.yml with the updated values.<\/strong><\/p>\n<figure id=\"attachment_139602\" aria-describedby=\"caption-attachment-139602\" style=\"width: 533px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Git-Actions-Python-package-file.png\"><img decoding=\"async\" class=\"wp-image-139602 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Git-Actions-Python-package-file.png\" alt=\"Git Actions Python package file\" width=\"543\" height=\"469\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Git-Actions-Python-package-file.png 543w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/Git-Actions-Python-package-file-300x259.png 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/a><figcaption id=\"caption-attachment-139602\" class=\"wp-caption-text\">Git Actions Python package file<\/figcaption><\/figure>\n<p><strong>Step 6: The commit in the previous page takes us to the GitActions jobs.<\/strong><\/p>\n<figure id=\"attachment_139603\" aria-describedby=\"caption-attachment-139603\" style=\"width: 640px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_ClickAgain.png\"><img decoding=\"async\" class=\"wp-image-139603 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_ClickAgain.png\" alt=\"Click Git Actions\" width=\"650\" height=\"158\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_ClickAgain.png 650w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2020\/08\/GitActions_ClickAgain-300x73.png 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-139603\" class=\"wp-caption-text\">Click Git Actions<\/figcaption><\/figure>\n<p><strong>Step 7:<\/strong> [Optional]<\/p>\n<p>On the Github Jobs page for the sample tutorial app, we can create a badge and place it on the README.md file for displaying the build status.<\/p>\n<p>Now, whenever the changes are committed to the master branch, Git Workflow, as written in python-package.yml will be followed and run on Git Actions.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we have covered all the base concepts from prerequisites to setting up CI\/CD workflow for a web application developed using Flask &#8211; A Python-based web development framework.<\/p>\n<p>This tutorial covers all necessary steps such as installing Python, downloading &amp; installing Flask, working with Flask-Appbuilder, testing with PyTest, etc. to get started with web development using Python. The web development community usually compares Flask with another popular Python web development framework called Django.<\/p>\n<p>We will explain these differences and will also compare these frameworks in one of the tutorials in this series.<\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"139588\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>This introductory Python Flask tutorial explains what is Flask, installation of Python, Virtualenv, Flask Hello World example with a section on code examples, debugging, and testing: Website development is more of an art than a skill. It requires patience and diligence, along with perseverance, courage, and dedication to create what&#8217;s &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Flask Tutorial &#8211; Introduction To Flask For Beginners\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/python-flask-tutorial\/#more-139588\" aria-label=\"Read more about Python Flask Tutorial &#8211; Introduction To Flask For Beginners\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":139672,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[407],"tags":[],"class_list":{"0":"post-139588","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-python"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/139588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=139588"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/139588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/139672"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=139588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=139588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=139588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}