项目5-Flask入门
安装镜像 参考:
https://blog.csdn.net/vinglemar/article/details/124698858
参考代码<下载>:
PrimeShow.py
from Lib import gcode from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') @app.route('/Index') def Index(): return render_template('Index.html') @app.route('/PrimeShow',methods = ['POST', 'GET']) def PrimeShow(): if request.method == 'POST': data= request.form imax=int(data["imax"]) primeList=gcode.getPrime(imax) return render_template("PrimeShow.html",primeList = primeList) if __name__ == '__main__': app.run(debug = True)
gcode.py
ef getPrime(imax): primeList=[2] bPrime=False for i in range(3,imax+1): bPrime=False for j in range(2,i): if i%j==0: bPrime=False break else: bPrime=True continue if(bPrime==True): primeList.append(i) return primeList print(getPrime(100))
Index.html:
DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>质数展示title>
<link rel="stylesheet" href="{{url_for('static',filename='bootstrap4/css/bootstrap.css')}}" type="text/css">
head>
<body>
<div class="container mt-2">
<form action="/PrimeShow" method="POST">
<div class="card" class="row">
<div class="card-header">
质数展示
div>
<div id="cardbody" class="card-body">
<h5 class="card-title">作者:gCode Teacher 标签h5>
<p id="tip" class="card-text">
<span class="badge badge-secondary">教师span>
<span class="badge badge-success">攻城狮span>
<span class="badge badge-danger">码农span>
<span class="badge badge-warning">体育爱好者span>
p>
<a href="http://www.exesoft.cn" class="btn btn-primary">官网:行易软件a>
div>
<div>
<div class="col input-group mb-4">
<div class="input-group-prepend">
<label class="input-group-text" for="imax">给一个最大范围label>
div>
<input id="imax" name="imax" step="1" min="0" max="100" type="number" value="0" class="form-control"/>
<input type = "submit" class="btn btn-primary" value = "submit" />
div>
form>
div>
body>
html>
PrimeShow.html:
DOCTYPE html>
<html>
<head>
<title>结果title>
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<link rel="stylesheet" href="{{url_for('static',filename='bootstrap4/css/bootstrap.css')}}" type="text/css">
head>
<body>
<div class="container mt-2">
<table class="table table-striped table-hover">
<tr>
<th class="bg-primary">顺序th><th class="bg-success">质数th><th class="bg-danger">备注th>
tr>
{% for prime in primeList %}
<tr>
<td>{{loop.index}}td><td>{{prime}}td>
<td>
{% if prime%10==9 %}
<span class="badge badge-warning">
末尾数为9,好数字!
span>
{% endif %}
td>
tr>
{% endfor %}
table>
div>
body>
html>