【问题标题】:Formatting Output in Flask & Python在 Flask 和 Python 中格式化输出
【发布时间】:2020-11-27 01:41:48
【问题描述】:

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

但是,我需要使用render_template 发布网站的输出,要让输出显示在文本输出中,我必须一次性传递所有内容。我使用列表执行此操作,但输出中的列表格式不正确。

我的网络应用:http://tomshoe02.pythonanywhere.com/scraper

示例输入:https://dtmwra1jsgyb0.cloudfront.net/groups/5e223506edae743046ecdaa2/matches

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

示例输出:['{{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...

期望的输出:

{{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=}}

app.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

【问题讨论】:

    标签: python list flask


    【解决方案1】:

    选项 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多