Flask和外部D

2024-06-02 06:58:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Anaconda并设置venv w/python 3.5,但如果需要,可以轻松更改版本——运行在windows 10

我的目标是创建具有以下布局的可共享文件:

shared_folder/
 ├── appLauncher.exe
 ├── config.py
 └── user_data/
     ├── images/
     └── database.db

其中,可执行文件读取config.py文件,config.py文件指示可执行文件从user_data文件夹读取数据(现在,我不使用配置文件,而是对路径进行硬编码——只是尝试让它工作)

我的当前目录如下所示:

shared_folder/
├── WebApp/
│    ├── static/
│    │    ├── css/
│    │    └── js/
│    ├── templates/
│    ├── __init__.py
│    └── routes.py
├── appLauncher.py
├── config.py
└── user_data/
     ├── images/
     └── database.db

我正在为两个问题而挣扎(我将此作为两个问题发布):

1.如何引导flask查看静态文件夹之外的内容并从user_数据文件夹读取数据?对于sqlite数据库和图像?

2. How can I package this using pyinstaller to allow me to reference config.py and the user_data files

当前分解示例(仅尝试从用户_数据中拉入一个图像):

appLauncher.py:

if __name__ == "__main__":
  from WebApp import app
  app.run()

static/css/play.css:

.helloworld {
  font-size: 80px;
}

模板/index.html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>my App</title>
  </head>
  <body>
    <link rel="stylesheet" href={{ url_for("static", filename="css/play.css") }}>
    <div class="helloworld">Hello World!</div>
    <!-- <img src='{{ url_for("static", filename="flask.jpg") }}'> -->
    <img src='{{ img }}'>
    <img src='{{ img|safe }}'>
  </body>
</html>

WebApp/init.py:

from flask import Flask
app = Flask(__name__)

import WebApp.routes

WebApp/routes.py:

from WebApp import app
from flask import render_template, url_for, send_file

@app.route("/")
@app.route("/index")
def hello():
    return render_template(
        "index.html",
        img = send_file(r"C:\Users\EvanS\Desktop\FlaskExternalEXE\user_data\flask.jpg")
    )

Tags: 文件frompyimportconfigappflaskimg