Weekly assignments. Total 4
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 secondnumber Create a simple Python driver to test the above class. get two numbers from the user 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 error 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 number 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 calculator """ Restful webservice for simple calculator """ 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/sub/14/7/ " retval += " to get the form /form " return retval @app.route("/form") def form(): return render_template("w3.html") @app.route('/mycalc/add/
//', methods=['GET', 'POST']) def add(fnum, snum): print("adding " + str(float(fnum)+float(snum))) return "adding " + str(float(fnum)+float(snum)) @app.route('/mycalc/sub///', 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 browsers 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
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 }) } Let’s explain what is going on a. The backend (Web service) @app.route('/mycalc/add///', methods=['GET', 'POST']) 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 server 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. b. 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 library as shown in tag. you="" need="" to="" convert="" these="" values="" to="" numeric="" before="" performing="" any="" math="" operations.="" b.="" 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="" library="" as="" shown="" in=""> you need to convert these values to numeric before performing any math operations. b. 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 library as shown in tag.>