Вы можете использовать сеансы. Например:
Python файл
Первая часть:
import io
from flask import Flask, render_template, session, send_file
import pandas as pd
app = Flask(__name__)
# Set the secret key to some random bytes and keep it secret.
# A secret key is needed in order to use sessions.
app.secret_key = b"_j'yXdW7.63}}b7"
Функция отправки:
@app.route("/submit", methods=["GET", "POST"])
def submit():
# Example dataframe
df = pd.DataFrame(
data={
"city": ["Seville", "London", "Berlin"],
"country": ["Spain", "United Kingdom", "Germany"]
}
)
# Store the CSV data as a string in the session
session["df"] = df.to_csv(index=False, header=True, sep=";")
return render_template("plot.html")
Функция загрузки:
@app.route("/download", methods=["POST"])
def download():
# Get the CSV data as a string from the session
csv = session["df"] if "df" in session else ""
# Create a string buffer
buf_str = io.StringIO(csv)
# Create a bytes buffer from the string buffer
buf_byt = io.BytesIO(buf_str.read().encode("utf-8"))
# Return the CSV data as an attachment
return send_file(buf_byt,
mimetype="text/csv",
as_attachment=True,
attachment_filename="data.csv")
Последняя часть:
if __name__ == "__main__":
app.run()
HTML файл ( шаблоны / график. html)
<body>
<form action="/download" method="POST">
<input type="Submit" value="Download CSV" >
</form>
</body>