โดย chatee supasand » 24/07/2019 5:39 pm
รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งานโปรแกรม
ไม่ทราบว่าเกินจากอะไรครับ python 2.7 windows 8.1
รันบน Command
- img1.png (10.82 KiB) Viewed 1409 times
แต่พอไปเปิดในเว็บไซต์
IOError
IOError: [Errno 2] No such file or directory: u'Show_rml/default.rml'
Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 2328, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 2314, in wsgi_app
response = self.handle_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1760, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Bell\PycharmProjects\generate_rml_to_pdf_miniproject1\index.py", line 24, in index
op = open('Show_rml/default.rml', 'r')
IOError: [Errno 2] No such file or directory: u'Show_rml/default.rml'
รูป
- img2.png (42.88 KiB) Viewed 1433 times
นี่พอรันใน pycharm กลับไม่ติด error
- img3.png (52.99 KiB) Viewed 1433 times
โค้ดโปรแกรมครับ
โค้ด: เลือกทั้งหมด
# -*- coding=utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, current_app, g
from flask import render_template
from flask import request,send_file
from flask_paginate import Pagination, get_page_args
import os
import os.path #import module เพื่อสร้าง path เก็บไฟล์
import psycopg2 as p
import random
from z3c.rml import rml2pdf
import click
click.disable_unicode_literals_warning = True
app = Flask(__name__)
app.config.from_pyfile('app.cfg')
con = p.connect(database='miniproject', user='openpg', password='openpgpwd')
cur = con.cursor()
@app.route('/')
def index():
op = open('Show_rml/default.rml', 'r')
re = op.read()
op.close()
return render_template('index.html', re=re)
@app.route('/', methods=['POST', 'GET'])
def addcode():
try:
if request.method == 'POST':
name = request.form['filename']
if name == '':
for i in range(1): # สุ่ม 1 ครั้ง
a = random.randint(10000000, 99999999) # ให้้สุ่มเลข
name = 'RmlToPDF' + str(a)
elif name != '':
name = request.form['filename']
des = request.form['description']
code = request.form['rml']
sql = "insert into genpdf (filename, description) values ('%s','%s')" % (name, des)
# sql = sql.encode('utf-8')
cur.execute(sql)
try:
sql = "SELECT * FROM genpdf ORDER BY fileid DESC"
sql = sql.encode('utf-8')
cur.execute(sql)
con.commit()
new_row_id = cur.fetchone()[0]
name = name.encode('utf-8')
save_path = './rmllist/' # สร้าง path สำหรับเก็บไฟล์
field_rml_name = str(new_row_id) + str(name) + '.rml'
path = os.path.join(save_path, field_rml_name) # สร้าง ตัวแปร เพื่อทำการเก็บที่อยู่ของไฟล์
f = open(path, 'w')
f.write(str(code))
f.close()
print ('สร้างไฟล์สำเร็จ')
d = open(path, 'r')
data = d.read()
d.close()
pdf = rml2pdf.parseString(data.decode("utf-8"))
open_path = str('./generate/') + str(name) + str('.pdf') # สร้าง path เพื่อเปิดไฟล์ที่เก็บไว้
a = open(open_path, 'w') # สร้างไฟล์ที่จากที่เก็บไว้ในตัว open_path
a.write(pdf.read())
a.close()
print data
print ('เพิ่มข้อมูล เรียบร้อยแล้ว')
pdfpath = ('./generate/' + str(name) + '.pdf')
resp = send_file(pdfpath)
open('./generate/' + str(name) + '.pdf')
resp.headers['Content-Disposition'] = "inline; filename=%s" % str(name) + str('.pdf')
resp.mimetype = 'application/pdf'
return resp
print('อ่านข้อมูล ผิดพลาด')
except:
con.rollback()
print('ERORR')
except:
con.rollback()
print ('database Error')
@app.route('/show') #กำหนด หน้า path
def show(): #def เป็นคำสำคัญสำหรับการสร้างฟังก์ชัน
cur.execute('select count(*) from genpdf')
total = cur.fetchone()[0]
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
sql = "SELECT * FROM genpdf ORDER BY fileid LIMIT {} OFFSET {}" \
.format(per_page, offset)
sql = sql.encode('ascii')
cur.execute(sql)
rows = cur.fetchall() # ทำการเลือกข้อมูลทั้งหมด
print rows
pagination = get_pagination(page=page,
per_page=per_page,
total=total,
record_name='users',
format_total=True,
format_number=True,
)
# return render_template('show.html', rows=rows)
return render_template('show.html', rows=rows,
page=page,
per_page=per_page,
pagination=pagination,)
def get_css_framework():
return current_app.config.get('CSS_FRAMEWORK', 'bootstrap4')
def get_link_size():
return current_app.config.get('LINK_SIZE', 'sm')
def get_alignment():
return current_app.config.get('LINK_ALIGNMENT', '')
def show_single_page_or_not():
return current_app.config.get('SHOW_SINGLE_PAGE', False)
def get_pagination(**kwargs):
kwargs.setdefault('record_name', 'records')
return Pagination(css_framework=get_css_framework(),
link_size=get_link_size(),
alignment=get_alignment(),
show_single_page=show_single_page_or_not(),
**kwargs
)
@click.command()
@click.option('--port', '-p', default=5000, help='listening port')
def run(port):
app.run(debug=True, port=port)
@click.command()
@click.option('--port', '-p', default=5000, help='listening port')
def run(port):
app.run(debug=True, port=port)
@app.route('/delete/<postID>', methods=['POST', 'GET'])
def delete(postID):
idd = postID
sql = "DELETE FROM genpdf WHERE fileid = '%s'" %idd
# sql = sql.decode('utf-8')
try:
cur.execute(sql)
con.commit()
print ('ลบข้อมูล เรียบร้อยแล้ว')
return '<br>'+'<META HTTP-EQUIV="Refresh" CONTENT="0;http://127.0.0.1:5000/show">'
except:
print('ลบข้อมูล ผิดพลาด ')
return 'Not Delete ID '
if con:
con.close()
@app.route('/opens/<postID>', methods=['POST', 'GET'])
def opens(postID):
idd = postID
try:
pdfpath = ('./generate/' + idd + '.pdf')
resp1 = send_file(pdfpath)
open('./generate/' + idd + '.pdf')
resp1.headers['Content-Disposition'] = "inline; filename=%s" % idd + str('.pdf')
resp1.mimetype = 'application/pdf'
except:
print('123 ผิดพลาด')
return resp1
@app.route('/edit/<postID>', methods=['POST', 'GET'])
def edit(postID):
idd = postID
sql = "SELECT * FROM genpdf WHERE fileid = %s " % (idd)
sql = sql.decode('utf-8')
cur.execute(sql)
rows = cur.fetchone()
save_path = './rmllist/'
field_rml_name = str(rows[0])+ str(rows[1]) + '.rml'
path = os.path.join(save_path, field_rml_name)
d = open(path, 'r')
data = d.read()
d.close()
return render_template("edit.html", row=rows, data=data)
@app.route('/editing/<postID>', methods=['POST', 'GET'])
def editing(postID):
if request.method == 'POST':
idd = postID
name = request.form['filename']
des = request.form['description']
sql = "UPDATE genpdf SET filename = '%s', description = '%s' WHERE fileid = '%s' " % (name, des, idd)
sql = sql.encode('utf-8')
try:
cur.execute(sql)
con.commit()
print ('เแก้ไขอมูล เรียบร้อยแล้ว')
return '<META HTTP-EQUIV="Refresh" CONTENT="0;http://127.0.0.1:5000/show">'
except:
con.rollback()
print('แก้ไขข้อมูล ผิดพลาด')
app.run(debug=True)
"""@app.route('/time')
def times:"""
[b][size=150]รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งานโปรแกรม[/size][/b]
ไม่ทราบว่าเกินจากอะไรครับ python 2.7 windows 8.1
[b]รันบน Command [/b]
[attachment=0]img1.png[/attachment]
[b]แต่พอไปเปิดในเว็บไซต์[/b]
[quote]IOError
IOError: [Errno 2] No such file or directory: u'Show_rml/default.rml'
Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 2328, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 2314, in wsgi_app
response = self.handle_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1760, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Bell\PycharmProjects\generate_rml_to_pdf_miniproject1\index.py", line 24, in index
op = open('Show_rml/default.rml', 'r')
IOError: [Errno 2] No such file or directory: u'Show_rml/default.rml'[/quote]
[b]รูป[/b]
[attachment=2]img2.png[/attachment]
[b]นี่พอรันใน pycharm กลับไม่ติด error[/b]
[attachment=1]img3.png[/attachment]
โค้ดโปรแกรมครับ
[code]# -*- coding=utf-8 -*-
from __future__ import unicode_literals
from flask import Flask, current_app, g
from flask import render_template
from flask import request,send_file
from flask_paginate import Pagination, get_page_args
import os
import os.path #import module เพื่อสร้าง path เก็บไฟล์
import psycopg2 as p
import random
from z3c.rml import rml2pdf
import click
click.disable_unicode_literals_warning = True
app = Flask(__name__)
app.config.from_pyfile('app.cfg')
con = p.connect(database='miniproject', user='openpg', password='openpgpwd')
cur = con.cursor()
@app.route('/')
def index():
op = open('Show_rml/default.rml', 'r')
re = op.read()
op.close()
return render_template('index.html', re=re)
@app.route('/', methods=['POST', 'GET'])
def addcode():
try:
if request.method == 'POST':
name = request.form['filename']
if name == '':
for i in range(1): # สุ่ม 1 ครั้ง
a = random.randint(10000000, 99999999) # ให้้สุ่มเลข
name = 'RmlToPDF' + str(a)
elif name != '':
name = request.form['filename']
des = request.form['description']
code = request.form['rml']
sql = "insert into genpdf (filename, description) values ('%s','%s')" % (name, des)
# sql = sql.encode('utf-8')
cur.execute(sql)
try:
sql = "SELECT * FROM genpdf ORDER BY fileid DESC"
sql = sql.encode('utf-8')
cur.execute(sql)
con.commit()
new_row_id = cur.fetchone()[0]
name = name.encode('utf-8')
save_path = './rmllist/' # สร้าง path สำหรับเก็บไฟล์
field_rml_name = str(new_row_id) + str(name) + '.rml'
path = os.path.join(save_path, field_rml_name) # สร้าง ตัวแปร เพื่อทำการเก็บที่อยู่ของไฟล์
f = open(path, 'w')
f.write(str(code))
f.close()
print ('สร้างไฟล์สำเร็จ')
d = open(path, 'r')
data = d.read()
d.close()
pdf = rml2pdf.parseString(data.decode("utf-8"))
open_path = str('./generate/') + str(name) + str('.pdf') # สร้าง path เพื่อเปิดไฟล์ที่เก็บไว้
a = open(open_path, 'w') # สร้างไฟล์ที่จากที่เก็บไว้ในตัว open_path
a.write(pdf.read())
a.close()
print data
print ('เพิ่มข้อมูล เรียบร้อยแล้ว')
pdfpath = ('./generate/' + str(name) + '.pdf')
resp = send_file(pdfpath)
open('./generate/' + str(name) + '.pdf')
resp.headers['Content-Disposition'] = "inline; filename=%s" % str(name) + str('.pdf')
resp.mimetype = 'application/pdf'
return resp
print('อ่านข้อมูล ผิดพลาด')
except:
con.rollback()
print('ERORR')
except:
con.rollback()
print ('database Error')
@app.route('/show') #กำหนด หน้า path
def show(): #def เป็นคำสำคัญสำหรับการสร้างฟังก์ชัน
cur.execute('select count(*) from genpdf')
total = cur.fetchone()[0]
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
sql = "SELECT * FROM genpdf ORDER BY fileid LIMIT {} OFFSET {}" \
.format(per_page, offset)
sql = sql.encode('ascii')
cur.execute(sql)
rows = cur.fetchall() # ทำการเลือกข้อมูลทั้งหมด
print rows
pagination = get_pagination(page=page,
per_page=per_page,
total=total,
record_name='users',
format_total=True,
format_number=True,
)
# return render_template('show.html', rows=rows)
return render_template('show.html', rows=rows,
page=page,
per_page=per_page,
pagination=pagination,)
def get_css_framework():
return current_app.config.get('CSS_FRAMEWORK', 'bootstrap4')
def get_link_size():
return current_app.config.get('LINK_SIZE', 'sm')
def get_alignment():
return current_app.config.get('LINK_ALIGNMENT', '')
def show_single_page_or_not():
return current_app.config.get('SHOW_SINGLE_PAGE', False)
def get_pagination(**kwargs):
kwargs.setdefault('record_name', 'records')
return Pagination(css_framework=get_css_framework(),
link_size=get_link_size(),
alignment=get_alignment(),
show_single_page=show_single_page_or_not(),
**kwargs
)
@click.command()
@click.option('--port', '-p', default=5000, help='listening port')
def run(port):
app.run(debug=True, port=port)
@click.command()
@click.option('--port', '-p', default=5000, help='listening port')
def run(port):
app.run(debug=True, port=port)
@app.route('/delete/<postID>', methods=['POST', 'GET'])
def delete(postID):
idd = postID
sql = "DELETE FROM genpdf WHERE fileid = '%s'" %idd
# sql = sql.decode('utf-8')
try:
cur.execute(sql)
con.commit()
print ('ลบข้อมูล เรียบร้อยแล้ว')
return '<br>'+'<META HTTP-EQUIV="Refresh" CONTENT="0;http://127.0.0.1:5000/show">'
except:
print('ลบข้อมูล ผิดพลาด ')
return 'Not Delete ID '
if con:
con.close()
@app.route('/opens/<postID>', methods=['POST', 'GET'])
def opens(postID):
idd = postID
try:
pdfpath = ('./generate/' + idd + '.pdf')
resp1 = send_file(pdfpath)
open('./generate/' + idd + '.pdf')
resp1.headers['Content-Disposition'] = "inline; filename=%s" % idd + str('.pdf')
resp1.mimetype = 'application/pdf'
except:
print('123 ผิดพลาด')
return resp1
@app.route('/edit/<postID>', methods=['POST', 'GET'])
def edit(postID):
idd = postID
sql = "SELECT * FROM genpdf WHERE fileid = %s " % (idd)
sql = sql.decode('utf-8')
cur.execute(sql)
rows = cur.fetchone()
save_path = './rmllist/'
field_rml_name = str(rows[0])+ str(rows[1]) + '.rml'
path = os.path.join(save_path, field_rml_name)
d = open(path, 'r')
data = d.read()
d.close()
return render_template("edit.html", row=rows, data=data)
@app.route('/editing/<postID>', methods=['POST', 'GET'])
def editing(postID):
if request.method == 'POST':
idd = postID
name = request.form['filename']
des = request.form['description']
sql = "UPDATE genpdf SET filename = '%s', description = '%s' WHERE fileid = '%s' " % (name, des, idd)
sql = sql.encode('utf-8')
try:
cur.execute(sql)
con.commit()
print ('เแก้ไขอมูล เรียบร้อยแล้ว')
return '<META HTTP-EQUIV="Refresh" CONTENT="0;http://127.0.0.1:5000/show">'
except:
con.rollback()
print('แก้ไขข้อมูล ผิดพลาด')
app.run(debug=True)
"""@app.route('/time')
def times:"""
[/code]