简体   繁体   English

如何使用python和Flask解析YAML文件并在网络上显示结果?

[英]How to parse YAML file using python and Flask and display the results on the web?

I would like to build a simple web application that able to display the results of parsing YAML files using python and Flask.我想构建一个简单的 Web 应用程序,它能够显示使用 python 和 Flask 解析 YAML 文件的结果。 I've been written the code and it works, but the results not the same as expected.我已经编写了代码并且它可以工作,但是结果与预期的不一样。 Here's my code:这是我的代码:

import yaml
from flask import Flask, request, render_template, redirect, url_for
#from pathlib import Path
app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def my_form_post():
    #path=Path('/Users/Devie Andriyani/EditFlask/categories.yaml') # set the path to your file here
    #if not path.exists():path.touch()
    if request.method == 'POST':
       #with open(path, 'w') as f:
           #f.write(request.form.get('Text Area',None))
        return redirect(url_for('my_form_post'))

    #with open(r'C:\Users\Devie Andriyani/EditFlask/categories.yaml') as f:
    #my_dict = yaml.safe_load(f)
    a_yaml_file = open("categories.yaml")
parsed_yaml_file = yaml.load(a_yaml_file, Loader=yaml.FullLoader)
print(parsed_yaml_file["countries"])
print(parsed_yaml_file["sports"])
return render_template('index.html')

if __name__ == '__main__':
   app.debug = True
    app.run()

And here's my HTML code:这是我的 HTML 代码:

 <!DOCTYPE html> <head> <title>Hello</title> </head> <body> <form action="" method="POST"> <p>Text Area</p> <p><textarea name="Text Area" rows="20" cols="50" value={{categories}}></textarea> </p> <input type="submit"> </form> </body> </html>

And here's my YAML file:这是我的 YAML 文件:

sports:
- soccer
- football
- basketball
- cricket
- hockey
- table tennis

countries:
- Pakistan
- USA
- India
- China
- Germany
- France
- Spain

And here's the result: I want the results of parsing show on the text area结果如下:我想在文本区域显示解析结果

You opened the file, saved it in a variable, but you didn't pass that to the front-end.您打开了文件,将其保存在一个变量中,但您没有将其传递给前端。 You just print it, that why it is printing in the console.您只需打印它,这就是它在控制台中打印的原因。 You have to pass this to the frontend.您必须将其传递给前端。

parsed_yaml_file = yaml.load(a_yaml_file, Loader=yaml.FullLoader)
# print(parsed_yaml_file["countries"]) # this will print in console not in frontend
# print(parsed_yaml_file["sports"])  # this too
return render_template('index.html', yml = parsed_yaml_file)

Here I passed the file content parsed_yaml_file to the frontend with the name yml .在这里,我将文件内容parsed_yaml_file传递给名为yml的前端。 So we can access this in the frontend now.所以我们现在可以在前端访问它。 But one problem we have.但是我们有一个问题。 yaml.load will return a dictionary. yaml.load将返回一个字典。 And if you want to display as a dictionary, then no worries.如果你想显示为字典,那就不用担心了。 But if you want to display as YAML format itself, then you should not convert it into yaml.但是如果你想显示为YAML格式本身,那么你不应该把它转换成yaml。 You directly pass the file a_yaml_file .您直接传递文件a_yaml_file

Suppose if you want yaml output, (hope you passed yml = a_yaml_file ) then in frontend you have to use pre tag.假设如果你想要 yaml 输出,(希望你通过yml = a_yaml_file )那么在前端你必须使用pre标签。

<pre>{{ yml }}</pre>

If you want a dictionary, (pass yml = parsed_yaml_file ) then just use this in frontend如果你想要一本字典,(通过yml = parsed_yaml_file )然后在前端使用它

{{ yml }}

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

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