如何使用Flask在开发模式下运行React?

时间:2018-12-12 17:03:56

标签: python python-3.x reactjs flask

我已经使用react-create-app来设置基本的React应用,并且通过设置生产模式和开发模式(相应地更改默认的static / template文件夹)来与Flask一起使用:

from datetime import datetime
from flask import Flask, render_template, url_for, request, redirect, abort, Response, send_file, send_from_directory, make_response
import argparse
import json
import logging
import pathlib
import sys
import os
import subprocess
import re
current_dir = os.path.dirname(os.path.realpath(__file__))
# Caminho para o diretório relativo ManagerFileIdentification
manager_dir = os.path.abspath(os.path.join(current_dir, "../"))
sys.path.append(manager_dir)

from InputDataController.libs.db_connection.dynamo.customer import Customer, StoneCustomer, CustomerManager
from InputDataController.libs.db_connection.dynamo.merchant import Merchant, StoneMerchant, MerchantManager
from InputDataController.libs.db_connection.dynamo.repository import Repository, RepositoryManager

from werkzeug.exceptions import BadRequest

DEVELOPMENT = {}

PRODUCTION = {
    "host": "0.0.0.0",
    "port": "80"
}

running_mode = PRODUCTION

if __name__ == '__main__':
    cmd_args = argparse.ArgumentParser(
        description="Gerenciador de adquirentes especiais da Concil.")
    cmd_args.add_argument(
        "--prod", help="Executa em modo de produção.", action="store_true")
    parsed_args = cmd_args.parse_args()
    appdata = ""
    if parsed_args.prod:
        appdata = "/home/ec2-user"
        app = Flask(__name__, template_folder = os.path.join(current_dir, "build").replace("\\", '/'))
        app.static_folder = os.path.join(current_dir, "build/static").replace("\\", '/')
    else:
        appdata = os.getenv("APPDATA")
        app = Flask(__name__, template_folder = os.path.join(current_dir, "public").replace("\\", '/'))
        app.static_folder = os.path.join(current_dir, "src").replace("\\", '/')

    @app.route('/acquirer-manager')
    def index():
        return render_template("index.html")

    app.config["TEMPLATES_AUTO_RELOAD"] = True
    app.config["appdata"] = appdata
    app.config["parsed_args"] = parsed_args

    if running_mode:
        app.run(running_mode["host"], running_mode["port"])
    else:
        app.run()

问题是,如果我运行npm run build(因此,以--prod模式运行flask,使用build文件夹中的static / templates内容),则只能看到编译后的react输出。这是一个问题,因为必须编译整个应用程序以查看前面的任何更改都非常无聊。而且... 我不想使用NodeJS或与NodeJS一起使用。如果在开发模式下用flask访问src和公用文件夹,则会遇到各种令人毛骨悚然的错误,因为文件显然是“原始的”。那么,如何在不进行每次编译的情况下运行带有react的flask?

编辑:This is ridiculous(我不想在脚本中显式使用babel标记。是否依赖NodeJS做出反应?)

0 个答案:

没有答案