วันนี้ผมมีความรู้เกี่ยวกับภาษา Python ที่จะมาแชร์ในเรื่องของการแสดงข้อความผิดพลาดจากโปรแกรมโดยใช้ ฟังก์ชั่น flash คือ ฟังก์ชั่นที่เรียกใช้จากโมดูล flask เข้ามาแล้วทำการ import ใช้งาน flash เป็นฟังก์ชั่นที่สามารถใช้ในการแสดงข้อความผิดพลาดได้จากโปรแกรม ก็จะมีรูปแบบดังนี้
โค้ดอันนี้เป็นโค้ดที่ไว้สำหรับตรวจเช็คค่าว่างที่ส่งมาจากหน้า home.html
โค้ด: เลือกทั้งหมด
from flask import Flask, render_template, request,flash
from flask_httpauth import HTTPBasicAuth
from flask import send_file
import subprocess
import os.path
base_store_pathpdf = '/home/com001/PycharmProjects/htmltoPdfImage2/pdf'
base_store_pathjpg = '/home/com001/PycharmProjects/htmltoPdfImage2/jpg'
app =Flask(__name__)
auth = HTTPBasicAuth()
app.secret_key = 'some_secret'
@auth.get_password
def get_password(username):
if username == 'mindphp':
return 'python'
return None
@app.route('/')
# @auth.login_required
def index():
return render_template('home.html')
@app.route('/view',methods=['GET'])
def convpdf():
filename = request.args.get('filename')
linkurl = request.args.get('linkurl')
typefile = request.args.get('typefile')
filename = filename.strip()
linkurl = linkurl.strip()
typefile = typefile.strip()
if not filename:
flash(' กรุณากรอกชื่อไฟล์ : filename is empty')
return render_template('home.html', linkurl=linkurl,typefile=typefile)
if not linkurl:
flash(' กรุณากรอกลิ้ง URL : linkurl is empty')
return render_template('home.html',typefile=typefile,filename=filename)
if not typefile:
flash(' กรุณากรอกเลือกประเภทการแปลงไฟล์ : typeconvert is empty')
return render_template('home.html',filename=filename,linkurl=linkurl)
if typefile == '1':
str = filename+'.pdf'
global base_store_pathpdf
path_bin = 'wkhtmltopdf'
path = os.path.join(base_store_pathpdf, str)
subprocess.call([path_bin, linkurl, path])
path_sh = 'pdf/' + str
response = send_file(path_sh)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline; filename=%s.pdf' % filename
for x in response.__dict__:
print(response.__dict__[x])
return response
elif typefile == '2':
str = filename + '.jpg'
global base_store_pathjpg
path_bin = 'wkhtmltoimage'
path = os.path.join(base_store_pathjpg, str)
subprocess.call([path_bin, linkurl, path])
path_sh = 'jpg/' + str
response = send_file(path_sh)
response.headers['Content-Type'] = 'image/jpg'
response.headers['Content-Disposition'] = 'inline; filename=%s.jpg' % filename
for x in response.__dict__:
print(response.__dict__[x])
return response
if __name__ == '__main__':
app.run(debug=True)
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body><br>
<div class="container-fluid">
<div class="row">
<div class="col-md-1" ></div>
<div class="col-md-10" >
<center><h4>เครื่องมือแปลงหน้าเว็บ HTML</h4></center>
<center><h4>PDF และ IMAGE</h4></center>
</div>
<div class="col-md-1" ></div>
</div>
</div>
<br>
<div class="container-fluid">
<div class="row">
<div class="col-md-3" ></div>
<div class="col-md-6" >
{%with messages = get_flashed_messages()%}
{%if messages %}
{% for message in messages%}
<div class="alert alert-warning alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Warning!</strong>{{message}}
</div>
{%endfor%}
{% endif %}
{%endwith%}
<div class="card">
<div class="card-body">
<form action="{{url_for('convpdf')}}" method="GET">
<div class="form-check-inline">
<div class="form-check">
<label class="form-check-label" >
<input type="radio" class="form-check-input"name="typefile" value="1" {%if typefile=='1'%}checked{%endif%} required>ไฟล์ PDF
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="typefile" value="2" {%if typefile=='2'%}checked{%endif%} required>ไฟล์ Image
</label>
</div>
</div>
<div class="form-group">
<label for="filename">ชื่อไฟล์ :</label>
<input type="text" name="filename" {%if filename%}value="{{filename}}"{%endif%} placeholder="" class="form-control" id="filename" required>
</div>
<div class="form-group">
<label for="linkurl">link url :</label>
<input type="text" name="linkurl" {%if linkurl%}value="{{linkurl}}"{%endif%} class="form-control" placeholder="www.example.com" id="linkurl" required>
</div>
<input type="submit" value="ตกลง" class="btn btn-success">
</form>
</div>
</div>
</div>
<div class="col-md-3" ></div>
</div>
</div>
</body>
</html>
โค้ด: เลือกทั้งหมด
from flask import Flask, render_template, request,flash
app.secret_key = 'some_secret'
if not filename:
flash(' กรุณากรอกชื่อไฟล์ : filename is empty')
return render_template('home.html', linkurl=linkurl,typefile=typefile)
if not linkurl:
flash(' กรุณากรอกลิ้ง URL : linkurl is empty')
return render_template('home.html',typefile=typefile,filename=filename)
if not typefile:
flash(' กรุณากรอกเลือกประเภทการแปลงไฟล์ : typeconvert is empty')
return render_template('home.html',filename=filename,linkurl=linkurl)
โค้ด: เลือกทั้งหมด
{%with messages = get_flashed_messages()%}
{%if messages %}
{% for message in messages%}
<div class="alert alert-warning alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Warning!</strong>{{message}}
</div>
{%endfor%}
{% endif %}
{%endwith%}
อ้างอิง
http://www.flask.pocoo.org/docs/1.0/patterns/flashing/
https://www.pythonprogramming.net/flash-flask-tutorial/
https://www.pythonise.com/feed/flask/flask-message-flashing