สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งานโปรแกรม

ไม่ทราบว่าเกินจากอะไรครับ python 2.7 windows 8.1

รันบน Command
img1.png
img1.png (10.82 KiB) Viewed 1396 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
img2.png (42.88 KiB) Viewed 1420 times
นี่พอรันใน pycharm กลับไม่ติด error
img3.png
img3.png (52.99 KiB) Viewed 1420 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:"""
แก้ไขล่าสุดโดย chatee supasand เมื่อ 24/07/2019 6:54 pm, แก้ไขไปแล้ว 2 ครั้ง.
รูปภาพ
tatiya
PHP VIP Members
PHP VIP Members
โพสต์: 2845
ลงทะเบียนเมื่อ: 09/07/2018 9:35 am

Re: Q - สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย tatiya »

โปรแกรมหน้า จะอ่านไฟล์ ในพาทไม่เจอ ตรง code บรรทัดที่ 24

โค้ด: เลือกทั้งหมด

op = open('Show_rml/default.rml', 'r')
    re = op.read()
ลองเปลี่ยนพาทเป็น ../Show_rml/default.rml
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: Q - สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

tatiya เขียน:โปรแกรมหน้า จะอ่านไฟล์ ในพาทไม่เจอ ตรง code บรรทัดที่ 24

โค้ด: เลือกทั้งหมด

op = open('Show_rml/default.rml', 'r')
    re = op.read()
ลองเปลี่ยนพาทเป็น ../Show_rml/default.rml
ลองเปลี่ยนแล้วครับ แต่ก็ยังหาไฟล์ไม่เจออยู่ดี
รูปภาพ
tatiya
PHP VIP Members
PHP VIP Members
โพสต์: 2845
ลงทะเบียนเมื่อ: 09/07/2018 9:35 am

Re: สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย tatiya »

ลองเป็น จุดเดียวครับ

./Show_rml/default.rml
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

tatiya เขียน:ลองเป็น จุดเดียวครับ

./Show_rml/default.rml
ยังไม่ได้อะครับ
รูปภาพ
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

โค้ด: เลือกทั้งหมด

try:
                    cur.execute(sql)
                    con.commit()
                    print ('ลบข้อมูล เรียบร้อยแล้ว')
                    return '<br>'+'<META HTTP-EQUIV="Refresh" CONTENT="0;http://127.0.0.1:5000/show">'

เขียนโค้ดไม่ควร FIX IP
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

แก้ได้แล้วครับ
ปัญหาเกิดจากการเรียก path ของตัวไฟล์ไม่ได้

แก้ไขโดยการ cd เรียกไฟล์เข้าไปในโฟล์เดอร์นั้น

โค้ด: เลือกทั้งหมด

cd  #ตามด้วยที่อยู่ของโปรแกรมเรา
รูปภาพ
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

ถามต่อครับ
กดแปลงไฟล์แล้วขึ้น error แบบนี้อะครับ ใน pycharm รันได้นะครับ
im4.png
im4.png (40.36 KiB) Viewed 1394 times
TypeError
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
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 1835, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 1850, in finalize_request
response = self.make_response(rv)
File "C:\Python27\lib\site-packages\flask\app.py", line 1976, in make_response
'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
รูปภาพ
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: สอบถามครับ รันโปรแกรมบน Command ใน Windows แต่โปรแกรม ERROR ตอนเข้าใช้งาน

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

แก้ได้แล้วครับ

เกิดจาดตัวของไฟล์ มี print ที่เป็นภาษาไทย
command ไม่สามารถอ่านภาษาไทยได้
ทำการลบออก หรือเปลี่ยนเป็นภาษาอังกฤษ ก็สามารถรันได้แล้ว
รูปภาพ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 85