Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

WEEK 2: - THIS ASSIGNMENT HAS ALREADY BEEN COMPLETED. INCLUDED FOR REFERENCE. This week you will review the techniques that Python uses to build data structures. Part 1: You are going to create the...

2 answer below »

WEEK 2: - THIS ASSIGNMENT HAS ALREADY BEEN COMPLETED. INCLUDED FOR REFERENCE.
This week you will review the techniques that Python uses to build data structures.
Part 1:
You are going to create the following:
Create a class with 4 private methods add, sub, mul, div and two public methods allInOneDict, and allinOneList
all methods will accept two parameters firstnumber, and secondnumbe
Create a simple Python driver to test the above class.
  get two numbers from the use
  and print the results of all operations(add, sub, mul, div) from
  allInOneDict, and allinOneList
  Organize and comment your code. The outputs must be labeled like below (see sample output)
  Note: methods of a class should not contain any UI functions
  the driver should contain al UI and e
or trapping.
Part 2:
You are going to enhance the prior assignment by doing the following:
1) Limit the input range from -100 to 100 for each input numbe
2) Prevent dividing by zero
3) Instead of hard coding values from -100 to 100, let the user enter the range
WEEK 3:
This week you will learn about web development and restful web services.
Part 1
You going to create a restful application with multiple routes to perform the simple calculations from prior week
You are going to have at least 7 routes, the default route is instructions for how to use the application
4 routes (add, sub, mult, and div)
2 routes (allInOneDIct, and allInOneList
Part 2
Create an HTML form to accept the 2 numbers and perform all the calculation in part I
WEEK 4:
This week you will learn about templating. Templating is a modern way to update the HTML form dynamically.
Part 1
You going to create a restful application with multiple routes to perform the simple calculations from prior week
You are going to have at least 7 routes, the default route is instructions for how to use the application
4 routes (add, sub, mult, and div)
2 routes (allInOneDIct, and allInOneList
Part 2
Create an HTML Template to accept the 2 numbers and perform all the calculation in part I
Hints: See the sample code in the lesson
WEEK 5:
This week you will learn about databases and SQL and create CRUD functions in SQL.
Download DB Browser for SQLite
this is a visual tool to create and manage your SQLite databases
You are going to add two routes to the restful application (created from week 4), one to save the calculation to the database, the second is to retrieve the calculations from the database. (add a button on the form from each route)
You are welcome to add your own features
WEEK 6:
This lesson you will learn about microservices and multiprocessing. Microservices is way to divide the restful series to smaller services. Multiprocessing is how to be able to run more that one task at the same time.
You are going to create two services. The first one with contain add, sub, mul, and div. The second service will contain allInoneDict and allInOneList. The second service will call the first service. Make sure either HTML form or the template is working.

Routing
Routing is a technique to access the desired page directly without having to navigate from the home page.
The route() decorator in Flask is used to bind URL to a function. From the example above;
@app.route("/")
def hello():
    return "Hello ENTD320!"
that means the default route will print “Hello ENTD320”
Let use create a simple routing for calculato
"""
  Restful webservice for simple calculato
"""
from flask import Flask, redirect, url_for, request, render_template
app = Flask(__name__)
@app.route("/")
def hello():
     retval  = "
Hello ENTD320, Welcome to my Restful webservices calculator

"
     retval += "Example;
"
     retval += " to add  two number  /mycalc/add/7/5/
"
     retval += " to subtract two number  /mycalc/su
14/7/
"
     retval += " to get the form /form
"
     return retval      
@app.route("/form")
def form():
     return render_template("w3.html")
@app.route('/mycalc/add
fnum
snum
', methods=['GET', 'POST'])
def add(fnum, snum):
    print("adding " + str(float(fnum)+float(snum)))
    return "adding " + str(float(fnum)+float(snum))
@app.route('/mycalc/su
fnum
snum
', methods=['GET', 'POST'])
def sub(fnum, snum):
    return "subtracting " + str(float(fnum)-float(snum))
if __name__ == '__main__':
    app.run(port=5200, debug=True)
to run the above code from any URL
(http:
localhost:5200/)
Note, most recent
owsers are tightening the security, so you will not be able to run forms out side the website to access another site this called XSS or cross site scripting.  For more information see; cross site scripting.
Below is the HTML/JavaScript page that will test the above web service
!--
  The code w3.html must be in folder called templates to work
--
html
  
ody
              

My Restful flask Calculator:
p
                    First Number             Second Number            Total                 
pre
                                         
p
     
form
  
ody
html
script src="https:
ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"
script
script
function doAct(lid)
{
    fnum=document.getElementById("fnm").value
    snum=document.getElementById("snm").value
    myform.action="http:
localhost:5200/ "+lid+"/"+fnum+"/"+snum+"/"
        $.get(myform.action, function(data, status){
                    alert("Data: " + data + "\nStatus: " + status);
                    alert(status);
                    alert(data)
                    document.getElementById("tnm").value=data
    })
}
script
Let’s explain what is going on
a. The backend (Web service)
@app.route('/mycalc/add
fnum
snum
', methods=['GET', 'POST'])
fnum> means an input to fnum variable, you can use any variables
 methods=['GET', 'POST'] these are the most common methods to submit data to the serve
def add(fnum, snum):
function to add to numbers from the request
    print("adding " + str(float(fnum)+float(snum))) 
this print for debug,
    return "adding " + str(float(fnum)+float(snum))
you need to convert these values to numeric before performing any math operations.
. The frontend
The above HTML form is a standard form with JavaScript function, the JavaScript function is called on each click
function doAct(lid)
{
    fnum=document.getElementById("fnm").value
    snum=document.getElementById("snm").value
    myform.action="http:
localhost:5200/ "+lid+"/"+fnum+"/"+snum+"/"
        $.get(myform.action, function(data, status){
                    alert("Data: " + data + "\nStatus: " + status);
                    alert(status);
                    alert(data)
                    document.getElementById("tnm").value=data
    })
}
The above function is taking the two input numbers and constructing the URL to the web services, then using the JQuery $.get() to capture the results from service. Make sure to include the link to the li
ary as shown in

Answered 39 days After Mar 08, 2022

Solution

Vicky answered on Apr 14 2022
113 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here