Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 95929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:44:06+00:00 2026-05-10T23:44:06+00:00

I want to create a very simple HTML/AJAX based GUI for a Python program.

  • 0

I want to create a very simple HTML/AJAX based GUI for a Python program. So the frontend is a HTML page which communicates with the program via AJAX. Can you give me a minimal implementation for the server-side using the python SimpleHTTPServer.SimpleHTTPRequestHandler?

A simple example would be a textfield and a button. When the button is pressed the content of the field is send to the server which then sends back a corresponding answer. I am aware that there are many powerful solutions for this in Python, but I would like to keep this very simple. I already found some nice examples for such a server (e.g. here), but so far I could not come up with a truly minimal one.

In case you wonder why I want to implement the GUI in such a way: My focus for this application is to display lots of data in a nice layout with only minimal interaction – so using HTML+CSS seems most convenient (and I have been already using it for non-interactive data display).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:44:07+00:00Added an answer on May 10, 2026 at 11:44 pm

    O.K., I think I can now answer my own question. Here is an example implementation for calculating the square of a number on the server. Please let me know if there are any improvements or misconceptions.

    the python server file:

    import threading import webbrowser import BaseHTTPServer import SimpleHTTPServer  FILE = 'frontend.html' PORT = 8080   class TestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):     '''The test example handler.'''      def do_POST(self):         '''Handle a post request by returning the square of the number.'''         length = int(self.headers.getheader('content-length'))                 data_string = self.rfile.read(length)         try:             result = int(data_string) ** 2         except:             result = 'error'         self.wfile.write(result)   def open_browser():     '''Start a browser after waiting for half a second.'''     def _open_browser():         webbrowser.open('http://localhost:%s/%s' % (PORT, FILE))     thread = threading.Timer(0.5, _open_browser)     thread.start()  def start_server():     '''Start the server.'''     server_address = ('', PORT)     server = BaseHTTPServer.HTTPServer(server_address, TestHandler)     server.serve_forever()  if __name__ == '__main__':     open_browser()     start_server() 

    …and the HTML file (I call it ‘frontend.html’, unfortunately the name has to appear in the JavaScript code as well):

    <html> <head> <title>AJAX test</title> </head> <body> <script type='text/javascript'>  function xml_http_post(url, data, callback) {     var req = false;     try {         // Firefox, Opera 8.0+, Safari         req = new XMLHttpRequest();     }     catch (e) {         // Internet Explorer         try {             req = new ActiveXObject('Msxml2.XMLHTTP');         }         catch (e) {             try {                 req = new ActiveXObject('Microsoft.XMLHTTP');             }             catch (e) {                 alert('Your browser does not support AJAX!');                 return false;             }         }     }     req.open('POST', url, true);     req.onreadystatechange = function() {         if (req.readyState == 4) {             callback(req);         }     }     req.send(data); }  function test_button() {     var data = document.test_form.test_text.value;                xml_http_post('frontend.html', data, test_handle) }  function test_handle(req) {     var elem = document.getElementById('test_result')     elem.innerHTML =  req.responseText }  </script>  <form name=test_form> sqr( <input type='text' name='test_text' value='0' size='4'> ) = <span id='test_result'>0</span> <input type=button onClick='test_button();' value='start' title='start'> </form>  </body> </html> 

    Of course it would be much more convenient to use jQuery for the XML request, but in the interest of simplicity I’ll leave it like that.

    Finally an alternative implementation using WSGI (unfortunately I didn’t see a way to fall back on the standard file-serving handler if the request is not a POST):

    import threading import webbrowser from wsgiref.simple_server import make_server  FILE = 'frontend.html' PORT = 8080  def test_app(environ, start_response):     if environ['REQUEST_METHOD'] == 'POST':         try:             request_body_size = int(environ['CONTENT_LENGTH'])             request_body = environ['wsgi.input'].read(request_body_size)         except (TypeError, ValueError):             request_body = '0'         try:             response_body = str(int(request_body) ** 2)         except:             response_body = 'error'         status = '200 OK'         headers = [('Content-type', 'text/plain')]         start_response(status, headers)         return [response_body]     else:         response_body = open(FILE).read()         status = '200 OK'         headers = [('Content-type', 'text/html'),                    ('Content-Length', str(len(response_body)))]         start_response(status, headers)         return [response_body]  def open_browser():     '''Start a browser after waiting for half a second.'''     def _open_browser():         webbrowser.open('http://localhost:%s/%s' % (PORT, FILE))     thread = threading.Timer(0.5, _open_browser)     thread.start()  def start_server():     '''Start the server.'''     httpd = make_server('', PORT, test_app)     httpd.serve_forever()  if __name__ == '__main__':     open_browser()     start_server() 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer To be fair, you can't blame it all on IE… May 11, 2026 at 3:45 pm
  • added an answer With facelets this would be cake. Since you are using… May 11, 2026 at 3:45 pm
  • added an answer I ended up talking to Esri support about this issue.… May 11, 2026 at 3:45 pm

Related Questions

I thought this would be simple, but I sure am having a lot of
Does anyone know whether a DOM Node of type Text is guaranteed not be
I'm playing around with Django on my website hosting service. I found out that
I've just started building a prototype application in Django. I started out by working

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.