Skip to main content

Posts

Showing posts with the label python flask

Test a FLASK Application

 TEST a FLASK Application Lets Write a Flask Application with multiple endpoints app.py from flask import Flask, jsonify, request app = Flask( __name__ ) @app.route ( '/' , methods = [ 'GET' ]) def hello ():     message = 'Welcome to Flask Application!'     return jsonify({ 'message' : message}) @app.route ( '/add/task' , methods = [ 'POST' ]) def add_task ():     data = request.get_json()     for d in data:         title = d.get( 'title' )         priority = d.get( 'priority' )         assignto = d.get( 'assignto' )         if ( len (title) == 0 or len (priority) == 0 or len (assignto) == 0 ):             message = 'Please fill all the required fields'             return jsonify({ 'message' : message}), 400             message = 'Task added succe...

Build Flask App(Kanbanboard) using Docker and Docker Compose

 A Kanban board is a project management tool designed to help visualize work, manage project tasks, workflows and communication. In this challenge, you are going to write the unit test cases for the given Kanban board Flask application using Pytest and deploy the application using Docker Compose. Instructions: 1. Write the unit test cases using Pytest testing framework in the following path 2. Refer 'test.py' file for further instructions. 3. Commands to Execute: - 'export FLASK APP-app.py' to set an environment variable. - 'python3 m flask run' to run the Flask application.  - 'python3 m pytest test.py' to run the test cases. 4. Once you done with test cases. Deploy the Kanban board Flask application in Docker using Docker Compose. 5. Create a 'Dockerfile' to dockerize the Kanban board Flask application with 'python alpine3.7 as its base image named 'kanbanboard_app_image".  6. Create 'docker-compose.yml file to create a s...

Testing a Flask App

 Main.py from flask import Flask from flask_restful import Api, Resource, reqparse app = Flask(__name__) api = Api(app) @app.route("/") def hello_world():     return "<p>Hello, World!</p>" class DemoApiEndpoint(Resource):     def __init__(self):         self.post_args = reqparse.RequestParser()         self.post_args.add_argument("name",                                     type=str,                                     help="You must include a name string with this post request.",                                     required=True)     def get(self):         return {             "message":...

Create & Dockerize a Flask app with Redis

  Create & Dockerize a Flask app with Redis For this project we would create everything from scratch. Section 1 : We would create the Flask app Section 2 : We would deploy the app as service, along with redis using docker and docker-compose. Section 1 Creating a flask app that returns the count number of times the page was visited. Create a  requirements.txt file, and paste the following dependency name Flask redis Import dependencies using pip install --no-cache-dir -r requirements.txt Create a  app.py file  from flask import Flask from redis import Redis app = Flask(__name__) redis = Redis(host='db_service', port=6379) @app.route('/') def hello():     visit_count = redis.incr('visit_count')     return f"Hello, this page has been visited {visit_count} times." if __name__ == '__main__':     app.run(host='0.0.0.0') We can test the app locally for this we need to: Install virtualenv if it's not already installed: sudo apt-get up...

Build Flask image using docker & docker-compose

 Build Flask Image using docker & docker-compose 1. Create a Dockerfile to build Flask image with Flask application, 'requirements.txt' and expose it in port 5000. 2. Create a 'docker-compose.json' file to create two services named 'python_web_app' and 'db_service' 3. The 'python_web_app' service  should be built from Dockerfile and specify the port as 5000 4. the 'db_service' should be built from image 'redis:alpine' 5. Add a volume to the 'python_web_app' service that mounts the current directory to the working directory inside the container and set the flask envireonment in debug mode. 6. Visit 'http:localhost:5000" to check weather the Flask application is running fine. The container name of 'python_web_app' service should be 'web_container' and container name of 'db_service' should be 'db_container' Here are the instructions for each of the tasks mentioned: Create a file na...