2012-07-12 82 views
2

我想要使用python,html和javascript构建桌面应用程序。到目前为止,我已经跟随了瓶子上的内容,并有一个你好世界工作的例子。我现在应该做些什么来使它工作? html文件如何与他们下面的python脚本“交谈”?Flask,html和javascript桌面应用程序

这里是到目前为止我的代码:

from flask import Flask, url_for, render_template, redirect 
app = Flask(__name__) 

@app.route('/hello/') 
@app.route('/hello/<name>') 
def hello(name=None): 
    return render_template('hello.html', name=name) 

@app.route('/') 
def index(): 
    return redirect(url_for('init')) 

@app.route('/init/') 
def init(): 
    css = url_for('static', filename='zaab.css') 
    return render_template('init.html', csse=css) 

if __name__ == '__main__': 
    app.run() 
+2

HTML文件从不与Python脚本“交谈”。 Python(通过Flask)将使用Jinja2使用传递给render_template()的任何信息来呈现HTML文件。你应该在这里完成教程:http://flask.pocoo.org/docs/tutorial/introduction/事情会在事后变得更有意义。 – EML 2012-07-12 16:29:41

+0

好吧,这是有道理的,但我怎么能通过数据呢?例如。通过一些表格的数据 – 2012-07-12 16:51:20

回答

2

您可以使用HTML表单,就像你通常会在神社的模板 - 然后在您的处理程序中使用下列内容:

from flask import Flask, url_for, render_template, redirect 
from flask import request # <-- add this 

# ... snip setup code ... 

# We need to specify the methods that we accept 
@app.route("/test-post", methods=["GET","POST"]) 
def test_post(): 
    # method tells us if the user submitted the form 
    if request.method == "POST": 
     name = request.form.name 
     email = request.form.email 
    return render_template("form_page.html", name=name, email=email) 

如果你想使用GET instaed POST提交表格,你只需检查request.args而不是request.form(有关更多信息,请参见flask.Request's documentation)。如果你打算用表格做很多事情,我建议你去看看优秀的WTForms项目和Flask-WTForms extension

+0

首先感谢你的回答,我是新的在烧瓶和忍者,所以我想知道如果我必须除了烧瓶安装忍者 – 2012-07-13 08:49:39

+1

@MpampinosHolmens - 如果你运行'pip install Flask'(假设你已经安装了pip并且可以在你的'PATH'上使用),那么Flask将会和它的依赖关系一起安装(Jinja2和Werkzeug)。 – 2012-07-13 16:16:44

+0

好的,非常感谢这就是我做的! – 2012-07-13 22:51:03