用于Python功能的Web UI [英] Web UI for Python function

查看:642
本文介绍了用于Python功能的Web UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python函数,它将一系列整数作为输入并返回另一个整数。我想以网络应用的形式发布这个功能。

登录页面应该包含一个带有一系列整数字段(带有输入验证)的web表单,下拉字段和一个提交按钮。提交按钮触发所述的python函数,并返回应该在html表中呈现的结果。



我是一个web开发的完全新手,但经过一番研究似乎烧瓶是我用于上述任务的最合适的框架。我的问题是,迄今为止我遇到的文档主要处理博客开发,因此与我以后的应用程序的类型不是特别相关的。



因此,我正在寻找任何指针(示例代码,书籍,文章)或指导,让我开始我的任务。以最简单的形式,我正在寻找的是:


  • 一个整数(1-10)和一个第二整数(1 - 5)
  • 如果用户输入无效的整数(< 1,> 10)

  • ,web表单将返回错误提交按钮python函数计算两个整数的总和
  • 结果显示在Web窗体上


    <


    解决方案

    好吧,这真的很简单,就是如何在html模板中呈现表单,您的视图来获取表单数据,并将上下文传递回模板。



    我很快地嘲笑一个样例,就像你想要的一样(没有什么奇怪的,只是回到基本的,并告诉你如何一起工作),它只是在两个文件 main.py (核心文件,像一个视图逻辑)和一个模板 calculation.html

    main.py rong>

     从瓶子导入Flask 
    从瓶子导入render_template $ b $从瓶子导入请求

    app = Flask(__ name__)

    @ app.route(/,methods = ['GET','POST'])
    def calculation():
    result = 0
    error =''
    #你可能想自定义你的GET ...在这种情况下不适用
    if request.method =='POST':
    #获取表单数据
    first = request.form ['first']
    second = request.form ['second']
    如果第一个和第二个:
    try:$
    if int(first)> 10或int(first)< 1:
    raise ValueError
    result = int(first) + int(second)
    除了ValueError:
    #你可能会传递自定义的错误信息
    error ='请输入1-10的整数'
    #you r输入模板并传递上下文结果&错误
    返回render_template('calculation.html',result = result,error = error)

    if __name__ ==__main__:
    app.run()

    templates / calculation.html

     < H1>计算与LT; / H1> 
    < form method =POST>
    < input type =textname =firstvalue =>
    < select name =second>
    < option value =1selected> 1< / option>
    < option value =2> 2< / option>
    < option value =3> 3< / option>
    < option value =4> 4< / option>
    < option value =5> 5< / option>
    < / select>
    < input type =submitvalue =Submit>
    {%如果结果%}
    < p>
    < label name ='result'>答案是:{{result}}< / label>
    < / p>
    {%endif%}
    {%if error%}
    < p>
    < label name =error> {{error}}< / label>
    < / p>
    {%endif%}
    < / form>

    希望这些都是自我解释的,你可以理解如何使用烧瓶和形式等。



    阅读 Flask Doc ,并试图通过,他们很简单,一旦你钉基本,你可能会开始寻找中间和前进的话题。

    WTForms
    的扩展,名为 Flask-跆拳道,它是非常方便的时候处理窗体,虽然没有什么能阻止你只是在像上面的代码一样的纯HTML格式的一切。



    希望这有助于我希望你喜欢简单而灵活 Flask 带给你的乐趣。


    I have a python function that takes a series of integers as inputs and returns another series of integers. I'd like to distribute the function in the form of a web app.

    The landing page should consist of a web form with a series of integer fields (with input validation), drop-down fields and a submit button. The submit button triggers the said python function and returns the results which should be rendered in an html table.

    I am a complete novice with web development, but after some research it appears that flask is the most appropriate framework for me to use for the above task. My problem is that the documentation I have encountered so far deals primarily with blog development and therefore not particularly relevant for the type of app I am after.

    I am therefore seeking any pointers (sample code, books, articles) or guidance to get me started with my task. In its simplest form, what I'm looking for is:

    • web form that takes one integer (1-10) and a second integer (1-5) from a drop down list
    • web form returns error if user enters invalid integer (<1, >10)
    • on submit button python function calculates the sum of the two integers
    • result is presented on the web form

    All guidance appreciated.

    解决方案

    Well it's quite simple really, it's all about how you present a form in html template, getting your view to get the form data, and passing the context back to the template.

    I've quickly mocked a sample like what you want (nothing fancy, just back to the basic and show you how these work together), it's only a few lines of code in 2 files main.py (core file, like a view logic) and a template calculation.html:

    main.py

    from flask import Flask
    from flask import render_template
    from flask import request
    
    app = Flask(__name__)
    
    @app.route("/", methods=['GET', 'POST'])
    def calculation():
        result = 0
        error = ''
        # you may want to customize your GET... in this case not applicable
        if request.method=='POST':
            # get the form data
            first = request.form['first']
            second = request.form['second']
            if first and second:
                try:
                    # do your validation or logic here...
                    if int(first)>10 or int(first)<1:
                        raise ValueError
                    result = int(first) + int(second)
                except ValueError:
                    # you may pass custom error message as you like
                    error = 'Please input integer from 1-10 only.'
        # you render the template and pass the context result & error
        return render_template('calculation.html', result=result, error=error)
    
    if __name__ == "__main__":
        app.run()
    

    templates/calculation.html

    <h1>Calculation</h1>
    <form method="POST">
        <input type="text" name="first" value="">
        <select name="second">
            <option value="1" selected>1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
        </select> 
        <input type="submit" value="Submit">
        {% if result %}
        <p>
            <label name='result'>Answer is: {{ result }}</label>
        </p>
        {% endif %}
        {% if error %}
        <p>
        <label name="error">{{ error }}</label>
        </p>
        {% endif %}
    </form>
    

    Hopefully these are self explanatory and you can get to understand how to work with the basic of Flask and forms etc.

    Read Flask Doc, and try to follow through, they are quite simple really and once you nail the basic, you may start looking for intermediate and advance topic.

    FYI, there is an extension for WTForms called Flask-WTF, it is very handy when dealing with forms, although nothing stops you just doing everything in plain html form like above code.

    Hope this helps and I wish you like the simplicity and flexiblity Flask brings you.

    这篇关于用于Python功能的Web UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆