4

I am trying to design and implement a basic calculator in HTML and Python(using CGI). Below given is a static HTML web page and it is being redirected to a python script (calci.py) where, I am able to calculate the sum but unable to append the resultant to the 'output' textbox.

calculator.html

<html>
<head>
    <title>Calculator</title>   
</head>   
<body>
    <form action="python_scripts/calci.py" method="post">
        Input 1 : <input type="text" name="input1"/><br>
        Input 2 : <input type="text" name="input2"/><br>    
        <input type="submit" value="+" title="add"  /><br>  
        output : <input type="text" name="output"/><br>
    </form>
</body>
</html>

calci.py

import cgi
form = cgi.FieldStorage() 

input1 = form.getvalue('input1')
input2  = form.getvalue('input2')

output = str(int(input1)+int(input2))
#how to return the response to the html
#and append it to the textbox

Thanks

2 Answers 2

3

This is not the way Web applications work - not hat simply, at least. If you want to rely only on the browser, and plain HTML for your application, each request has to send the whole html page as a string. You have to use Python's string formatting capabilities to put the resulting number in the correct place in the HTML form.

This way of working is typical of "Web 1.0" applications (as opposed to the "Web 2.0" term used about ten years ago). Modern web applications use logic that runs on the client side, in Javascript code, to make an HTTP request to retrieve only the needed data - and them, this client-side logic would place your result in the proper place in the page, without reloading the page. This is what isgenerally known as "ajax". It is not that complex, but the html + javascript side of the application become much more complex.

I think one should really understand the "Web 1.0" way before doing it the "Ajax way". in your case, let's suppose your HTML containing the calculator form is in a file called "calc.html". Inside it, where the result should lie, put a markup that can be understood by Python's built-in native string formatting methods, like {result} -

<html>
<body>
...
calculator body
...
Answer: <input type="text" readonly="true" value={result} />
</body>
</html>

And rewrite your code like:

import cgi
form = cgi.FieldStorage() 

input1 = form.getvalue('input1')
input2  = form.getvalue('input2')

result = int(input1)+int(input2)
html = open("calc.html".read())
header = "Content-Type: text/html; charset=UTF-8\n\n"

output = header + html.format(result=result)
print (output)

The CGI way is outdated, but is nice for learning: it relies on your whole program being run, and whatever it prints to the standard output to be redirected to the HTTP request as a response. That includes the HTTP Headers, which are included, in a minimal form, above.

(I will leave the complete implementation of a way for the raw '{result}' string not to show up in the inital calculator form as an exercise from where you are 0- the path is to get the initial calculator html template through a CGI script as well, instead of statically, (maybe the same) as well - and just populate "result" with "0" or an empty string)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response. can you give any references to learn about web 2.0 way and also about django web server.
0

you can transfer response with the help of java script. use under print("window.location=url")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.