diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-09-08 21:59:27 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-09-08 21:59:27 +0800 |
commit | 811c5afcc09f1080466f204b4e1b83a6018733d9 (patch) | |
tree | add0d7ccd61822fca65b94a61a8cef2d912c801c | |
parent | a11a1ceac9799efc85a3722a7a95fdecd7e88cf3 (diff) |
Serve from /xlsx
-rw-r--r-- | __main__.py | 26 | ||||
-rw-r--r-- | templates/index.html | 6 |
2 files changed, 24 insertions, 8 deletions
diff --git a/__main__.py b/__main__.py index 1902718..de9540c 100644 --- a/__main__.py +++ b/__main__.py @@ -1,4 +1,13 @@ -from flask import Flask, render_template, request, redirect, flash, send_file, after_this_request +from flask import ( + Flask, + render_template, + request, + redirect, + flash, + send_file, + after_this_request, + send_from_directory, +) import os import logging import secrets @@ -7,22 +16,29 @@ from pathlib import Path from .compress import compress UPLOAD_FOLDER = Path(gettempdir()) / "risksheet" +URL_PATH = "/xlsx" # https://example.com/xlsx app = Flask(__name__) app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER # NOTE: the secret key will be regenerated each time this application is run # this is ok because we don't store credentials in the cookie app.secret_key = secrets.token_hex() +app.static_url_path = "xlsx/static" -logging.basicConfig(filename=UPLOAD_FOLDER / "risksheet.log") +logging.basicConfig(filename=UPLOAD_FOLDER / "risksheet.log", level=logging.WARNING) -@app.get("/") +@app.get(URL_PATH + "/static/<path:name>") +def subpath_static(name): + return send_from_directory("static", name) + + +@app.get(URL_PATH) def index(): - return render_template("index.html") + return render_template("index.html", path=URL_PATH) -@app.post("/") +@app.post(URL_PATH) def sheet(): # receive and validate file try: diff --git a/templates/index.html b/templates/index.html index c5edd1f..ad48dc1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ <html> <head> <title>高中风险地区表格压缩器</title> - <link rel="stylesheet" href="/static/index.css" + <link rel="stylesheet" href="{{ path }}/static/index.css" </head> <body> <h1>高中风险地区表格压缩器</h1> @@ -15,7 +15,7 @@ </ul> {% endif %} {% endwith %} - <form method="post" action="/" enctype="multipart/form-data"> + <form method="post" action="{{ path }}" enctype="multipart/form-data"> <input name="file" type="file" @@ -27,7 +27,7 @@ <summary>点击查看使用说明</summary> <h3>表格格式</h3> <figure> - <img src="/static/sheet.png" /> + <img src="{{ path }}/static/sheet.png" /> </figure> <p> 图中高亮部分是压缩器会读取的数据,其他部分将会被忽略,但是黄色部分不能删除。 |