简体   繁体   English

链接HTML Tornado服务器和Python文件的方法

[英]Methods on linking a HTML Tornado server and Python file

This is my sample HTML file 这是我的示例HTML文件

    <html>
    <head>
    <title>
    </title>
    </head>
    <body>
    <form action="">
    Value a:<br>
    <input type="text" name="Va">
    <br>
    Value b:<br>
    <input type="text" name="Vb">
    <br><br>
    <input type="submit">
    </form>
    <textarea rows="4" cols="10">

    </textarea>
    <p>
    </p>
    </body>
    </html>

And a given template Tornado server code:(I also need help on the explanation of each section of the following code) 和给定的模板Tornado服务器代码:(我还需要帮助解释以下代码的每个部分)

    import tornado.ioloop
    import tornado.web
    import tornado.httpserver
    import tornado.gen
    import tornado.options
    tornado.options.parse_command_line()  
    class APIHandler(tornado.web.RequestHandler):
        @tornado.web.asynchronous
        def get(self):
            self.render('template.html')
        @tornado.gen.engine
        def post(self):
            try:
                num = int(self.get_argument('num'))
            except:
                num = 5
            self.render('template.html')
    app = tornado.web.Application([(r"/next_rec",APIHandler),])        
    if __name__ == "__main__":
        server = tornado.httpserver.HTTPServer(app)
        server.bind(48763)
        server.start(5)
        tornado.ioloop.IOLoop.current().start()

and finally my python code: 最后我的python代码:

    if __name__ == '__main__':
        a = int(raw_input())
        b = int(raw_input())
    print a+b

I am using a simple 'a+b' function to test out this feature. 我正在使用一个简单的'a + b'函数来测试这个功能。 But my problem is I can't figure out a way to link them together. 但我的问题是我无法找到将它们连接在一起的方法。 So my ultimate goal is to click on the "Submit" button on the HTML, pass on two values to the Tornado server, use it as input in my python script and finally show the output in the text area of the HTML or on another page. 所以我的最终目标是点击HTML上的“提交”按钮,将两个值传递给Tornado服务器,在我的python脚本中将其用作输入,最后在HTML的文本区域或另一页面上显示输出。 I'm know there are tons of information on the web, but I'm completely new to Tornado (near 0 knowledge) and most of them I can't really understand. 我知道网上有大量的信息,但我对Tornado来说是全新的(接近0知识),其中大多数我都无法理解。 Help on methods or keywords for search is much appreciated, thank you very much. 非常感谢您对搜索方法或关键字的帮助,非常感谢。 (please keep answers as basic as possible, it will help a lot, thanks!) (请尽可能保持答案基础,这将有很大帮助,谢谢!)

First of all you should check the official documentation . 首先,您应该查看官方文档 It is quite simple and it targets the newcomers. 这很简单,它针对的是新人。

Also in this short guide , the sections of a similar code as your is being explained with simplicity. 同样在这个简短的指南中 ,与您的类似代码的部分正在简单地解释。

Now for your code: 现在为您的代码:

On your template you need to specify that the form should send a post request on submit by adding <form method="post" id="sum_form"> 在您的模板上,您需要指定表单应通过添加<form method="post" id="sum_form">发送提交请求

Also you need to make sure that you will be submit the data added in the form on an event: $("#sum_form").submit(); 此外,您需要确保在事件中提交表单中添加的数据: $("#sum_form").submit();

On your post method you need to read the passed numbers from your client's form, add them and then send them back to the template as a parameter. 在post方法中,您需要从客户端表单中读取传递的数字,添加它们,然后将它们作为参数发送回模板。

For example: 例如:

def post(self):
  numA = int(self.get_argument('Va'))
  numB = int(self.get_argument('VB'))
  sumAB = numA + numB
  self.render('template.html',sumAB=sumAB)

In you template.html you need to add a field where you will display the passed sum as a jinja variable : {{sumAB}} 在template.html中,您需要添加一个字段,您将把传递的总和显示为jinja变量: {{sumAB}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM