Create Arithmetic Calculator

To create an arithmetic calculator using request arguments in Flask, we can follow these steps:

  • Create a new file calculator.py and define a route for the calculator in Flask app. This route will accept request arguments for the numbers and operation to be performed. For example:

Python3




@app.route('/calculate')
def calculate():
   
  # Get the first operand using request arguments
  a = request.args.get('a')
   
  # Get the Second operand using request arguments
  b = request.args.get('b')
   
  # Get the operation to be perform
  operator = request.args.get('operator')


  • Now, the parameters we have are in string type. To convert them to integers we can use the int() function. Also, we will ensure that the arguments should not empty.

Python3




# Make sure that the request arguments are not empty
  if a and b and operator:
    # Convert the request arguments to integers
    a = int(a)
    b = int(b)


  • Use an if statement to determine which operation to perform based on the value of the operator request argument. We can use the standard arithmetic operators (+, -, *, /) to perform the desired operation.

Python3




# Perform the requested operation
    if operator == 'add':
      result = a + b
    elif operator == 'subtract':
      result = a - b
    elif operator == 'multiply':
      result = a * b
    elif operator == 'divide':
      result = a / b


  • Finally, Return the result of the operation to the user. Otherwise, if any argument is missing in the URL then return the “Error: Insufficient arguments”

Python3




  return f'{a} {operator} {b} = {result}'
else:
  return 'Error: Insufficient arguments'


Here is a Complete code:

Python3




from flask import Flask, request
 
app = Flask(__name__)
 
@app.route('/calculate')
def calculate():
   
  # Get the first operand using request arguments
  a = request.args.get('a')
   
  # Get the Second operand using request arguments
  b = request.args.get('b')
   
  # Get the operation to be perform
  operator = request.args.get('operator')
 
  # Make sure that the request arguments are not empty
  if a and b and operator:
    # Convert the request arguments to integers
    a = int(a)
    b = int(b)
 
    # Perform the requested operation
    if operator == 'add':
      result = a + b
    elif operator == 'subtract':
      result = a - b
    elif operator == 'multiply':
      result = a * b
    elif operator == 'divide':
      result = a / b
 
    return f'{a} {operator} {b} = {result}'
  else:
    return 'Error: Insufficient arguments'
 
  app.run()


To use this calculator, we can send a GET request to the ‘/calculate’ endpoint with the request arguments ‘a’, ‘b’, and ‘operator’.

For example, to add 2 and 3, we can send a request like this:

http://localhost:5000/calculate?a=2&b=3&operator=add

 

To Multiply 4 and 7:

http://localhost:5000/calculate?a=2&b=3&operator=multiply 

 

Similarly, we can perform Subtraction and Division.

If any of the arguments are missing in the URL, Like

http://localhost:5000/calculate

Or

http://localhost:5000/calculate?a=2&b=3

 



Using Request Args for a Variable URL in Flask

This article will teach us how to use Request Arguments in our Flask application. First, we will understand. What are the Request Arguments?

Similar Reads

What are the Request Arguments?

Request Arguments are MultiDict objects with the parsed contents of the query string (the part in the URL after the question mark)....

Create Arithmetic Calculator

...