Pycharm demo项目
app2.py(运行)
index2.html (网页)
网页访问地址: http://127.0.0.1:5000
网页画面
核心代码(网页)
- 点击按钮弹窗选择 txt 文件(index2.html)
<form method="post" enctype="multipart/form-data">
<p class="font-weight-bold">上传文本(只支持.txt)</p>
<input type="file" name="file"/><br><br>
<input type="submit" class="btn btn-primary font-weight-bold text-white" value="生成内容"/>
</form>
- 获取 txt 文件(app2.py)
file = request.files["file"]
- 保存文件到预设的文件目录下,直接 open read 读取,得到文本内容 a(app2.py)
if file:
print(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
print('C:/Users/'+username+'/Desktop/'+file.filename)
f = open('C:/Users/'+username+'/Desktop/'+file.filename, "r+", encoding='utf-8')
filecontent = f.read()
a = str(filecontent)
- 将文本内容 a 发送到index2.html网页(app2.py)
return render_template('index2.html', a=a)
- 网页获取文本内容并显示(index2.html)
<div>
<h2>文本内容 :</h2>
<p class=" font-italic lead text-justify">{{ a }}</p>
</div>
index2.html
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
</head>
<body class="maincon">
<header>
<div class="jumbotron text-center">
<h1>demo</h1>
</div>
</header>
<div class="container p-3 my-3 text-white text-justify" >
<div class="row">
<div class="col-xl-12">
<div class="form-group">
<h2> </h2>
<form method="post" enctype="multipart/form-data">
<p class="font-weight-bold">上传文本(只支持.txt)</p>
<input type="file" name="file"/><br><br>
<input type="submit" class="btn btn-primary font-weight-bold text-white" value="生成内容"/>
</form>
</div>
</div>
</div>
<br>
<div>
<h2>文本内容 :</h2>
<p class=" font-italic lead text-justify">{{ a }}</p>
</div>
<br>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
app2.py
from flask import Flask, render_template, request, redirect, abort, flash
from textblob import TextBlob
from spellchecker import SpellChecker
import re
import getpass
import os
username = getpass.getuser()
UPLOAD_FOLDER = 'C:/Users/'+username+'/Desktop/demo'#创建保存txt的文件夹
print(UPLOAD_FOLDER)
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
app.config['UPLOAD_EXTENSIONS'] = ['.txt']
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/", methods=["GET", "POST"])
def index():
a=""
if request.method == "POST":
print("FORM DATA RECEIVED")
if "file" not in request.files:
flash('No file part')
return redirect(request.url)
'''获取文件'''
file = request.files["file"]
if file.filename == "":
flash('No selected file')
return redirect(request.url)
if file.filename != '':
file_ext = os.path.splitext(file.filename)[1]
if file_ext not in app.config['UPLOAD_EXTENSIONS']:
print("Please upload a .txt file type only")
return abort(400)
if file:
print(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
print('C:/Users/'+username+'/Desktop/'+file.filename)
f = open('C:/Users/'+username+'/Desktop/'+file.filename, "r+", encoding='utf-8')
filecontent = f.read()
a = str(filecontent)
return render_template('index2.html', a=a)
if __name__ == "__main__":
app.run(debug=True, threaded=True)