如何在Flask中打开创建的本地页面

时间:2018-12-08 05:07:40

标签: python-3.x flask

我使用了烧瓶render_template,并在本地创建了一个页面(order_html)。但是,我不知道如何从浏览器中打开order_html。我怀疑它应该是localhost:5000/html_pages/order_job_id.html,但没有用。

打开本地存储的新创建网页的最终正确方法是什么?最终,我将在互联网上发布该应用程序,我应该能够通过www.domain.com/order_job_id.html

打开这些创建的网页
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, send_from_directory
import os
import sys
import json
import uuid
import logging

base_dir = sys.path[0]
temp_dir = os.path.join(base_dir, 'pages')
log_main_file = os.path.join(base_dir, 'logs', 'log.log')
main_logger = setup_logger('main_log', log_main_file)
download_path = os.path.join(base_dir,'html_pages')
html_page = os.path.join(base_dir, 'pages')
logo_file = os.path.join(base_dir, 'docs', 'logo.txt')

app = Flask(__name__, template_folder = temp_dir)
app.config.from_object(__name__)
app.config["DEBUG"] = True

@app.route('/parse', methods=['GET', 'POST'])
def sendgrid_parser():
    att_metadata = request.form.get('attachment-info', '')
    job_id = str(uuid.uuid4().hex)
    job_dir = os.path.join(download_path, job_id)
    if len(request.form) > 0 and len(att_metadata) > 0:
        # Set up logger for parsing emails
        os.mkdir(job_dir)
        job_log = os.path.join(download_path, job_id, 'log.log')
        email_logger = setup_logger('email_log', job_log)
        main_logger.info('Job [' + job_id + '] is created.')
        att_info = json.loads(att_metadata)
        att_count = int(request.form.get('attachments')) if request.form.get('attachments') else 0
        # Process attachments if any
        if att_count > 0:            
            for num in range(1, (att_count + 1)):
                request.files['attachment%d' % num].save(os.path.join(job_dir,file_name))
                email_logger.info(prefix + 'File ['+file_name+'] is downloaded.')

            email_logger.info(prefix + 'Email data has been parsed and loaded into the database.')

            rendered = render_template('order.html', img_data=img_data, job_id=job_id)

            order_html = os.path.join(job_dir,'order_' + job_id + '.html')
            if len(rendered) > 0:
                try:
                    output = open(order_html, 'w')
                    output.write(rendered)
                    output.close()
                    email_logger.info(prefix + 'Order page for ' + job_id + ' is generated successfully.')

                    return 'Good'

                except Exception as e:
                    email_logger.error(prefix + 'Saving order html failed! File Path:[' + order_html + '] Error: ' + str(e))
            else:
                order_html = 'Blank'
                email_logger.error(prefix + 'Something is wrong.')

    else:
        main_logger.error(prefix + 'There is nothing to process.')
    return send_from_directory(job_dir, '/order_' + job_id + '.html')

if __name__ == '__main__':
    app.run()

0 个答案:

没有答案