繁体   English   中英

在 Flask & Python 中格式化 Output

[英]Formatting Output in Flask & Python

我的 web 应用程序已启动并运行。

但是,我需要使用render_template发布网站的 output 要让 output 在文本 output 中显示,我必须一次性通过。 我使用列表执行此操作,但列表在 output 中的格式不正确。

我的 web 应用程序: http://tomshoe02.pythonanywhere.com/scraper

样本输入: https://dtmwra1jsgyb0.cloudfront.net/groups/5e223506edae743046ecdaa2/matches

示例 output(屏幕截图): https://i.imgur.com/J2yOc3K.png

示例 output: ['{{MatchSchedule|team1=UAlbany eSports|team2=Thomas Esports|team1score=2|team2score=0|winner=1|date=2020-02-07|time=13:00|timezone=PST|dst=yes|vod1=|stream=}}\n', '{{MatchSchedule|team1=King Tornado|team2=|team1score=|team2score=|winner=|date=2020-02-08|time=12:00|timezone=PST|dst=yes|vod1=|stream=}}\n', '{{Ma...

所需的 output:

{{MatchSchedule|team1=UAlbany eSports|team2=Thomas Esports|team1score=2|team2score=0|winner=1|date=2020-02-07|time=13:00|timezone=PST|dst=yes|vod1=|stream=}}

{{MatchSchedule|team1=King Tornado|team2=|team1score=|team2score=|winner=|date=2020-02-08|time=12:00|timezone=PST|dst=yes|vod1=|stream=}}

应用程序.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
import os, sys, subprocess, importlib
from stuff import jloader

directory = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SECRET_KEY'] = 'tomisgoated'

class BattlefyForm(FlaskForm):
    matchhistory = StringField('Match History Link')

@app.route("/test")
def home():
    return render_template("base.html")

@app.route("/", methods=["GET", "POST"])
@app.route("/lol", methods=["GET", "POST"])
@app.route("/scraper", methods=["GET", "POST"])
def scraper():
    form = BattlefyForm()
    if form.validate_on_submit():
        url = form.matchhistory.data
        result = jloader(url)
        '''
        #result = url
        result = subprocess.check_output([sys.executable,
            "{}/stuff.py".format(directory), url]).decode('iso-8859-1')
        '''
        return render_template("scraper.html", form=form, result=result)
    return render_template("scraper.html", form=form)

@app.route('/base', methods=["GET"])
def base():
    return render_template("base.html")

@app.template_filter('nl2br')
def nl2br(s):
    return s.replace("\n", "<br />")

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

stuff.py: https://www.codepile.net/pile/Ore8pj5e

选项 1:在传递给模板之前将列表转换为字符串。

list1 = ['a','b','c']
print("\n".join(list1))

选项 2:将值按原样传递给模板并使用 Jinja 修复

在这里进行更多讨论: https://stackoverflow.com/a/3971100/12939068

<div>
{% for item in list %}
    <div>{{ item }}</div>
{% endfor %}
</div>

暂无
暂无

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

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