烧瓶webapp上的多个按钮?

时间:2015-04-29 03:10:49

标签: python-2.7 flask flask-wtforms

我正在使用python和flask制作我的第一个webapp,它是一个简单的计算器,但我目前卡住尝试使用多个按钮。一开始只是为了显示图形,这里是python代码:

class FormulaForm(Form):
    formula = StringField('formula')
    graph = SubmitField('graph')

@app.route('/')
def calculate():
    form = FormulaForm()
    formula = request.args.get('formula','')
    points = mp.make_points(formula,0,7)
    comp = make_plot(points[0],points[1])
    return render_template('index.html',the_script=comp[0],the_div=comp[1],form=form)

这是html代码:

<form method="GET" action="">
    <br />
        {{ form.formula }}
    <br />
        {{ form.graph }}
</form>

到目前为止一切顺利。但我不知道如何添加更多功能,例如我想添加一个按钮,显示在某个值x处评估的公式。我尝试在表单中添加一个额外的输入字段和一个额外的按钮,如下所示:

class FormFormula(Form):
    formula = StringField('formula')
    graph = SubmitField('graph')
    evaluate = StringField('evaluate_at')
    evaluate = SubmitField('evaluate')

但后来我不知道如何让视图处理两个不同的动作。 我想我找到了一个解决方案here,但只有当方法是“POST”并且使页面重新加载时我才不会想要它。那么,有没有办法在同一个视图中使用多个按钮?

1 个答案:

答案 0 :(得分:0)

@app.route('/start' ,methods=['POST'])
def stop():
    "process done here"
    return Something

你的app.py就像这样和html文件这个

    <script src="static/js/ajax.js"></script>
    <script type="text/javascript" language="javascript">
    $(document).ready(function() {
    $("#start").click(function(event){
    $.post(
        "/start",
        function(data) {
            window.alert(data);         
            }
        );      
            });
    });

</script>

<button id ="start" type="button" value = "Load Data">Start</button>
相关问题