Вы можете использовать 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, как я описал в предыдущем ответе.