Ittociwam
Ittociwam

Reputation: 91

Python CGI replaces my html

I am teaching myself CGI with python and I am having trouble finding any references on how to just insert the output of the python program into my current html instead of replacing the whole existing page.


Here is the python:

#!python
import cgi, cgitb
cgitb.enable()

form = cgi.FieldStorage()

def loan(r, term, pv):
   if r is not None:
      r = float(r)
   if term is not None:
      term = float(term)
   if pv is not None:
      pv = float(pv)
   rate = r / 12
   numPeriods = 12 * term
   rate /= 100
   monthlyPayment = (pv * rate) / (1 - (1 + rate) ** (numPeriods * -1))
   return round(monthlyPayment, 2)

def getInput():
   r = form.getvalue("rate")
   term = form.getvalue("term")
   pv = form.getvalue ("amount")
   monthlyPayment = loan(r, term, pv)
   print("<p>Monthly Payment: %.2f<p>" % monthlyPayment)


print "Content-type: text/html\r\n\r\n"   
print """
<div>
<h1> Python CGI </h1>
<br />
 """

getInput()

print """
<br />
</div>
""" 

And the HTML:

<DOCTYPE html>
<html>
<head>
<title> Python CGI Test </title>
</head>
<body>

<h1> Mortage Loan Calculator CGI</h1>

<form action ="/cgi-bin/myPython.py" method ="get">

Rate: <input type = "text" name = "rate"/> <br />

Term: <input type = "text" name = "term"/> <br />

Amount <input type = "text" name = "amount"/> <br />

<button type = "submit" > submit </button>
</form>
</body>
</html>

I would like the html from the python script to insert just below the form.

Upvotes: 2

Views: 402

Answers (1)

jkd
jkd

Reputation: 1045

This is Server Side which means once the page is loaded, the only ways to reload content from Python is through reloading or AJAX. All the code will be run before the content is sent back to the user.

Upvotes: 1

Related Questions