Python之Flak入门

Posted xueyuehua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之Flak入门相关的知识,希望对你有一定的参考价值。

文件名vsearch4web

from flask import Flask, render_template, request
from vsearch import search4letters

app = Flask(__name__)

@app.route(/search4, methods=[POST])
def do_search() -> html:
    phrase = request.form[phrase]
    letters = request.form[letters]
    title = Here are your results:
    results = str(search4letters(phrase, letters))
    return render_template(results.html,
                           the_phrase=phrase,
                           theletters=letters,
                           the_thtle=title,
                           the_results=results)


@app.route(/)
@app.route(/entry, methods=[GET, POST])
def entry_page() -> html:
    return render_template(entry.html, the_title=Welcome to search4letters ont the web!)

app.run

文件名vsearch

def search4vowels(phrase: str) -> set:
    """Return any vowels found in a supplied phrase."""
    vowels = set(aeiou)
    return vowels.intersection(set(phrase))


def search4letters(phrase: str, letters: str=aeiou) -> set:
    """Return any vowels found in a supplied phrase."""
    return set(letters).intersection(set(phrase))

在temolates中的三个html文件

  results

{% extends base.html %}
{% block body %}
    <h2>{{ the_title }}</h2>
    <p>You submitted the following Date:</p>
    <table>
        <tr>
            <td>Phrase:</td>
            <td>{{ the_phrase }}</td>
        </tr>
        <tr>
            <td>Letter:</td>
            <td>{{ the_letters }}</td>
        </tr>
    </table>
    <p>When "{{ the_phrase }}" is search for "{{ the_letters }}",the following results are returned:</p>
    <h3>{{ the_results }}</h3>
{% endblock %}

  entry

{% extends base.html %}

{% block body %}

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

    <form method=POST action=/search4>
        <table>
            <p>Use this form to submit a search request:</p>
            <tr>
                <td>Phrase:</td>
                <td><input name=phrase type=TEXT width=60></td>
            </tr>
            <tr>
                <td>Letters:</td>
                <td><input name=letters type=TEXT value=aeiou></td>
            </tr>
        </table>
        <p>When youre ready, click this button:</p>
        <p><input value=Do it! type=SUBMIT></p>
        <p><input value="Do it!" type="SUBMIT"></p>
    </form>

{% endblock %}

  base

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ the_title }}</title>
    <link rel="stylesheet" href="static/hf.css"/>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>

在static中的css样式

  hf

  

技术分享图片
body {
  font-family:      Verdana, Geneva, Arial, sans-serif;
  font-size:        medium;
  background-color: tan;
  margin-top:       5%;
  margin-bottom:    5%;
  margin-left:      10%;
  margin-right:     10%;
  border:           1px dotted gray;
  padding:          10px 10px 10px 10px;
}
a {
  text-decoration:  none; 
  font-weight:      600; 
}
a:hover {
  text-decoration:  underline;
}
a img {
  border:           0;
}
h2 {
  font-size:        150%;
}
table {
  margin-left:      20px;
  margin-right:     20px;
  caption-side:     bottom;
  border-collapse:  collapse;
}
td, th {
  padding:          5px;
  text-align:       left;
}
.copyright {
  font-size:        75%;
  font-style:       italic;
}
.slogan {
  font-size:        75%;
  font-style:       italic;
}
.confirmentry {
  font-weight:      600; 
}

/*** Tables ***/

table {
font-size:          1em;
background-color:   #fafcff;
border:             1px solid #909090;
color:              #2a2a2a;
padding:            5px 5px 2px;
border-collapse:    collapse;
}

td, th {
border:             thin dotted gray;
}

/*** Inputs ***/
input[type=text] {
  font-size:        115%;
  width:            30em;
}
input[type=submit] {
  font-size:        125%;
}
select {
  font-size:        125%;
}
View Code

 

以上是关于Python之Flak入门的主要内容,如果未能解决你的问题,请参考以下文章

Python 之 Socket编程(TCP/UDP)

Python入门基础之条件判断和循环

移动测试之appium+python 入门代码

移动测试之appium+python 入门代码

Python之如何优雅的重试

Java基础入门五)之方法以及递归算法