对于Flask的更新处理

对原有的代码进行了更新,并加入新的HTML页面,关注注释和理解等有空了在补上,现在天天跑工地挺累的

对vsearch4web页面实行新增在吗
from flask import Flask, render_template, request, escape
from vsearch import search4letters

app = Flask(__name__)


def log_request(req: 'flask_request', res: str) -> None:
    with open('vsearch.log', 'a') as log:
        # print(req, res, file=log)
        # print(str(dir(req)),res,file=log)
        print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|')


@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    """Extract the posted data; perform the search; return results."""
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = 'Here are your results:'
    results = str(search4letters(phrase, letters))
    log_request(request, results)
    return render_template('results.html',
                           the_title=title,
                           the_phrase=phrase,
                           the_letters=letters,
                           the_results=results, )


@app.route('/viewlog')
def view_the_log() -> 'html':
    contents = []
    with open('vsearch.log')as log:
        # contents = log.read()
        # contents=log.readlines()
        for line in log:
            contents.append([])
            for item in line.split('|'):
                # contents.append(escape(item))
                contents[-1].append(escape(item))
    titles = ('Form Data', 'Remote_addr', 'User_agent', 'Results')
    print(titles)
    print(contents)
    return render_template('viewlog.html',
                           the_title='View Log',
                           the_row_titles=titles,
                           the_data=contents, )


@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
    """Display this webapp's HTML form."""
    return render_template('entry.html',
                           the_title='Welcome to search4letters on the web!')


app.run

新的界面在templates中加入viewlog

{% extends 'base.html' %}

{% block body %}

    <h2>{{ the_title }}</h2>

    <table>
        <tr>
            {% for row_title in the_row_titles %}
                <th>{{ row_title }}</th>
            {% endfor %}
        </tr>
        {% for log_row in the_data %}
            <tr>
                {% for item in log_row %}
                    <td>{{ item }}</td>
                {% endfor %}

            </tr>
        {% endfor %}
    </table>

{% endblock %}

结果界面

猜你喜欢

转载自www.cnblogs.com/XueYueHua/p/10176113.html
今日推荐