0

単純な「投票」Webページがあり、ユーザーには3列のテーブルが表示されます。各列には検索エンジンクエリの結果が含まれ、ユーザーはどの列がより良い結果をもたらすかを選択し、ボタンをクリックします。

サンプルは次のとおりです:http://jsfiddle.net/rfeGa/

次の点についてサポートが必要です。1。htmlページとpythonプログラムの間の投票を追跡するにはどうすればよいですか。2.クエリのリストをPythonファイルに保持したいのですが、その情報をWebページに渡すにはどうすればよいですか?

これが私の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

以下のjqueryの基本的な部分を使用するようにjsfiddleを変更しました。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 webimportpyodbcから入力を取得しています。

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()

mod_WSGIを使用して呼び出されたPythonファイル呼び出し。以前の回答で説明したように、Apache + mod_WSGI+web.pyバックエンドを設定する方法を見てください。

于 2012-03-03T22:12:50.900 に答える