【发布时间】:2015-06-03 19:02:09
【问题描述】:
我想编写一个 python 脚本来将我(从脚本)得到的结果输出到外部文本框。
就像其他程序的文本框或网页的搜索栏一样。
【问题讨论】:
标签: python-3.x textbox output
我想编写一个 python 脚本来将我(从脚本)得到的结果输出到外部文本框。
就像其他程序的文本框或网页的搜索栏一样。
【问题讨论】:
标签: python-3.x textbox output
如果你想把flask放到网页上,试试flask。
要设置您的数据库(例如sqlite),请使用以下内容:
# creating database
import sqlite3 as lite
import sys
con = None
try:
con = lite.connect('test.db')
cur = con.cursor()
cur.execute('CREATE DATABASE HELLOWORLD;')
cur.execute('USE DATABASE HELLOWORLD;')
cur.execute('CREATE TABLE HELLOWORLD.MYDATA(ID INT PRIMARY KEY AUTOINCREMENT, CONTENT TEXT NOT NULL,);')
except lite.Error, e:
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
然后更改您的脚本,以便将其结果写入此数据库(根据其网站,许多进程可以访问 sqlite 数据库,但一次只能写入一个):
# insert values into db
a = ['this is a triumph', 'im making a note here', 'huge success']
for el in a:
cur.execute('insert into HELLOWORLD.MYDATA values (?)', el)
然后让烧瓶端上来。要让烧瓶查看数据库,您需要进行一些初始设置:
# more copy pasta from http://flask.pocoo.org/docs/0.10/patterns/sqlite3/
import sqlite3
from flask import g
DATABASE = '/path/to/test.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
但是你可以将这些信息提供给浏览器(穿上你最漂亮的 CSS 裤子):
# check out the demo at http://flask.pocoo.org/ to see where this goes
@app.route('/')
def index():
cur = get_db().cursor()
cur.execute('SELECT ALL FROM HELLOWORLD.MYDATA')
return "<p>{}</p>".format(cur.fetchall())
免责声明,我只是在过去的几分钟内把它放在一起,我还没有真正测试过这些。但这绝对应该让你开始。
【讨论】: