How can I display a website with html forms locally using python and collect user input?

I do research and usually collect data, allowing participants to perform some tasks on the computer and record their answers (I write programs using the PsychoPy wrapper ). That is, the program runs locally, and the data is stored locally.

Now, I would like to know if there is a way to use Python to display a (local) website with html forms for the user and collect input (locally). The reason for this idea is that currently, when I want to display checkboxes, radio objects or input fields, I use wxPython. This works pretty well, but programming and layout in wxPython is pretty cumbersome, and I would prefer html with forms.

The requirement would be that he would need a rum without any borders, address field, menu bar ... The reason is that I need it as a full-screen mode (currently I open a window without pop-ups in the window screen size to hide the desktop) so that participants can do nothing but work with forms.

So, I’m looking for a way: (a) display html sites, including the html form above the piglet window, without a menu bar or in general, (b) collect the input data by clicking the Ok button (that is, the form is sent), (c) control what is presented before and after viewing this website, and (d) it should all happen locally!

My idea is that data is collected when participants click the submit button in the following pic example and the next page is displayed.

example form

Update: I am using windows (XP or 7).

+2
source share
5 answers

This is a solution that uses Qt Webkit to render HTML. The default navigation request handler terminates with a function that validates submitted form requests. The form uses the get method, so the data is included in the request URL and can be restored in this way. The original request is rejected, and you can change the contents of the displayed web page as you wish.

from PyQt4 import QtGui, QtWebKit app = QtGui.QApplication([]) view = QtWebKit.QWebView() # intercept form submits class MyWebPage(QtWebKit.QWebPage): def acceptNavigationRequest(self, frame, req, nav_type): if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted: text = "<br/>\n".join(["%s: %s" % pair for pair in req.url().queryItems()]) view.setHtml(text) return False else: return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type) view.setPage(MyWebPage()) # setup the html form html = """ <form action="" method="get"> Like it? <input type="radio" name="like" value="yes"/> Yes <input type="radio" name="like" value="no" /> No <br/><input type="text" name="text" value="Hello" /> <input type="submit" name="submit" value="Send"/> </form> """ view.setHtml(html) # run the application view.show() app.exec_() 
+4
source

As AdamKG noted, using webframework would be a good choice. Since Django and the like can be crowded here using micro-webframework, for example ' flask ' or ' bottle ' would be a great choice.

This link demonstrates through a step-by-step instruction how to make a simple form through the To-DO application. He accepts zero prior knowledge.

You can run it only locally.

+4
source

you need a simple solution, so just write an http server and run your simple page.

using python.BaseHTTPServer encoding a web server 15 lines :

 import BaseHTTPServer class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): if self.path == '/foo': self.send_response(200) self.do_something() else: self.send_error(404) def do_something(self): print 'hello world' server = BaseHTTPServer.HTTPServer(('',80), WebRequestHandler) server.serve_forever() 

simple enough, but I suggest using some web frameworks. They are also lightweight.

e.g. web.py. here is what you want in 50 lines :

  • install web.py
  • create a directory with two files:

     ./ |-- app.py `-- templates `-- index.html 
  • index.html

    $ def with (form, ret)

     <html> <head> <title> another site </title> </head> <body> <h1> hello, this is a web.py page </h1> <form action="" method="post"> $:form.render() </form> <h2>$:ret</h2> </body> </html> 
  • Logic file app.py:

     import web ### Url mappings urls = ( '/', 'Index', ) ### Templates render = web.template.render('templates') class Index: form = web.form.Form( web.form.Textbox('fav_name', web.form.notnull, description="Favorite Name:"), web.form.Textbox('cur_name', web.form.notnull, description="Current Name:"), web.form.Button('Send Away'), ) def GET(self): """ Show page """ form = self.form() return render.index(form, "") def POST(self): """ handle button clicked """ form = self.form() if not form.validates(): return render.index(form, "INPUT ERROR") # save data by ur method, or do some task #pyglet.save_data(form.d.fav_name, form.d.cur_name) #pyglet.draw(some_pic) #os.system(some_cmd) form = self.form() return render.index(form, "YOUR DATA SAVED") app = web.application(urls, globals()) if __name__ == '__main__': app.run() 
  • start this server in your windows:

    python app.py 9999

  • open browser: http://127.0.0.1:9999/

By the way, if ur data is just strings, you can save it in web.by by sqlite.

+3
source

My suggestion would be:

  • Use some python server like SimpleHTTPServer . This is necessary because the submit button on the form sends information to the server. There you must somehow manage the information received;
  • Your browser is configured with one of these Kiosk extensions, which prohibits the use of Alt + F4. An example is the Kiosk extension for Firefox
  • If you wish, if you have any similarities with scripts in general, you can create a script that, when launched, will simultaneously launch the python server and open your html file in a browser. This will facilitate your setup work for each object in your group.

EDITOR: I read that you need a pinglet in a browser window. This could be included in the script in step 3 using the “always on top” option and absolute positioning of the piglet (I can say that this will probably be easier on Linux, which can be run by persistent LiveUSB - just a thought!)

EDIT (regarding a published comment): I think that the most reliable option for output would be a disk (file or database) or RAM (running a python object), after which you will read the information from the file. Then, in case of unexpectedness (system freezing, power failure), already entered data will be there.

The only (and most important) part that I don't know. HOW to do this in order to process the contents of the submit form on the server side. It is likely that a server script (php, python) will be created and left on the server’s root, so the server will receive an HTTP request containing information and send the information to a script, which then processes the processing and storage of files / databases.

This may interest you: "The POST request method is used when the client needs to send data to the server as part of the request, for example, when downloading a file or sending a completed form." (from wikipedia to "POST (HTTP)" ENTRY)

In another link, some thoughts on using SimpleHTTPServer to process POST requests: http://islascruz.org/html/index.php/blog/show/Python%3A-Simple-HTTP-Server-on-python..html

Hope this helps.

+1
source

The reason for this idea is that currently whenever I want to display checkboxes, radio buttons or input fields I use wxPython. This works pretty well, but programming and layout in wxPython is cumbersome, and I would prefer html with forms.

You can combine the lightness of HTML and create custom Windows applications using Flex with the Python backend.

If you do not belong to Flex, a little more is involved, but still the built-in Windows Camelot application generator

Edit

Instead of typing it again - I would suggest the django + flex + pyamf article in Adobe, which explains all this with screenshots. You can replace django with a jar or bottle as they are lighter, however the PyAMF library provides built-in support for django, which is why it was used in this example.

PyAMF provides an "Action Message Format" (a binary protocol for exchanging objects with the Flash runtime) for Python.

+1
source

All Articles