0

我有一个简单的“投票”网页,其中向用户展示了一个包含三列的表格。每列将包含搜索引擎查询的结果,然后用户将选择哪一列具有更好的结果并单击按钮。

这是一个示例:http: //jsfiddle.net/rfeGa/

我需要以下帮助: 1. 如何跟踪 html 页面和 python 程序之间的投票?2. 我想在 python 文件中保留查询列表,我如何将该信息传递给网页?

这是我的html页面:

<!DOCTYPE html>
<html>
    <head>
        <title>Search Engine Comparator!</title>
    </head>
    <body>      
        % queries = ("ccny", "bmcc", "qcc");
        % bingScore = googScore = yhooScore = 0;        
        % for q in queries:
        The query is: {{ q }}
        <br />
        And the scores are: Bing = {{ bingScore }}, Google = {{ googScore }}, Yahoo = {{ yhooScore }}.
        <hr>    
        <form action="/" method="post">
            <table border="1" width="100%">         
                <tr>
                    <th>
                        <input type="submit" name="engine1" value="Engine 1 is better!" />
                    </th>
                    <th>
                        <input type="submit" name="engine2" value="Engine 2 is better!" />
                    </th>
                    <th>
                        <input type="submit" name="engine3" value="Engine 3 is better!" />
                    </th>       
                </tr>
            </table>
        </form>
        % end
    </body>
</html>     

<script type="text/javascript">

</script>

这是我的python代码:

from bottle import request, route, run, view
from mini_proj import *

@route('/', method=['GET', 'POST'])
@view('index.html')
def index():
    return dict(parts = request.forms.sentence.split(),
                show_form = request.method == 'GET');

run(host = 'localhost', port = 9988);
4

1 回答 1

2

您可以使用 jquery + mod_WSGI 让 Python 成为 Javascript 前端的后端。例如,您可以将结果推送回 Python 脚本并让它修改数据库。以下是解决方案的一些简化部分。我遗漏了一些细节。

我不熟悉bottle,但我web.py过去曾帮助完成此任务。

我修改了您的jsfiddle以使用以下 jquery 中的基本部分。您的 javascript 将返回单击了哪个按钮。

<script type="text/javascript">

jQuery(document).ready(function() {

    jQuery("input ").click(function() {

        // this captures passes back the value of the button that was clicked.
        var input_string = jQuery(this).val();
        alert ( "input: " + input_string );

        jQuery.ajax({
            type: "POST",
            url: "/myapp/",
            data: {button_choice: input_string},
        });
    });
});

</script>

并让您的 python 脚本在后端修改数据库。

这里是使用 web.py ,从 jQuery import web import pyodbc 获取输入

urls = ( '/', 'broker',)
render = web.template.render('/opt/local/apache2/wsgi-scripts/templates/')

application = web.application(urls, globals()).wsgifunc()

class broker:
    def GET(self):
        # here is where you will setup search engine queries, fetch the results 
        # and display them

        queries_to_execute = ...
        return str(queries_to_execute)
    def POST(self):
        # the POST will handle values passed back by jQuery AJAX

        input_data = web.input()

        try:
            print "input_data['button_choice'] : ", input_data['button_choice']
            button_chosen = input_data['button_choice']
        except KeyError:
            print 'bad Key'


        # update the database with the user input in the variable button_chosen

        driver = "{MySQL ODBC 5.1 Driver}"
        server = "localhost"
        database = "your_database" 
        table = "your_table"

        conn_str = 'DRIVER=%s;SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % ( driver, server, database, uid, pwd ) 

        cnxn = pyodbc.connect(conn_str)

        cursor = cnxn.cursor()

        # you need to put some thought into properly update your table to avoid race conditions
        update_string = "UPDATE your_table SET count='%s' WHERE search_engine='%s' "

        cursor.execute(update_string % (new_value ,which_search_engine)
        cnxn.commit()

您的 python 文件调用使用 mod_WSGI 调用。看看如何设置 Apache + mod_WSGI + web.py 后端,就像我在之前的答案中描述的那样。

于 2012-03-03T22:12:50.900 回答