如何使用python在本地显示具有html表单的网站并收集用户输入? [英] How do I display a website with html-forms locally using python and collect the user input?

查看:84
本文介绍了如何使用python在本地显示具有html表单的网站并收集用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名行为科学家,通常通过让参与者在计算机上完成一些任务并记录他们的响应来收集数据(我使用pyglet包装器编写程序 PsychoPy ).也就是说,程序在本地运行,数据存储在本地.

I am a behavorial scientist and usually collect data by letting participants do some tasks on a computer and record their responses (I write the programs using the pyglet wrapper PsychoPy). That is, the program runs locally and the data is stored locally.

现在,我想知道是否有一种方法可以使用Python向用户显示带有html表单的(本地)网站,并(本地)收集输入.这种想法的原因是,当前每当我要显示复选框,单选按钮或输入字段时,我都会使用wxPython.效果很好,但是wxPython中的编程和布局有点麻烦,我更喜欢带表单的html.

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

一个要求是它将需要没有任何边框,地址字段,菜单栏等的朗姆酒.原因是我需要在全屏模式下使用它(我目前在非全屏pygflet窗口中打开屏幕尺寸以隐藏桌面),以便参与者只能在表格上进行任何操作.

A requirement would be that it would need to rum without any borders, adress field, menu bar, ... The reason is that I need it in kind of fullscreen mode (I currently open a non-fullscreen pygflet window in the size of the screen to hide the desktop) so that participants can do nothing but work on the forms.

因此,我正在寻找一种方法(a)在没有菜单栏或任何菜单的pyglet窗口上方显示html表单,包括html表单;(b)单击确定"按钮时收集输入(例如,表单已发送),(c)控制在查看本网站之前和之后显示的内容,以及(d)所有这些都应在本地进行!

我的想法是,当参与者在下面的示例图片中单击发送"按钮并显示下一页时,将收集数据.

My idea would be that the data is collected when participants hit the "Send away" button in the following example pic and the next page is displayed.

更新:我使用Windows(XP或7).

Update: I use windows (XP or 7).

推荐答案

这是使用Qt Webkit呈现HTML的解决方案.默认的导航请求处理程序由检查提交的表单请求的函数包装.该表单使用"get"方法,因此数据包含在请求的url中,并且可以通过这种方式进行检索.原始请求被拒绝,您可以根据需要更改显示的网页的内容.

This is a solution using Qt Webkit for rendering HTML. The default navigation request handler is wrapped by a function that checks for submitted form requests. The form uses the "get" method, so the data is included in the url of the request and can be retrieved that way. The original request is declined and you can change the content 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_()

这篇关于如何使用python在本地显示具有html表单的网站并收集用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆