Spaces:
Sleeping
Sleeping
Commit
·
58bde6b
1
Parent(s):
815a086
After fix
Browse files- app.py +27 -19
- templates/plot.html +8 -4
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template, request,
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
|
@@ -6,14 +6,13 @@ import io
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
-
app.config["UPLOAD_FOLDER"] = "uploads"
|
| 10 |
-
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
def process_files(golden_file, test_file):
|
| 16 |
-
|
| 17 |
limits_df1 = pd.read_excel(golden_file, nrows=4)
|
| 18 |
df1 = pd.read_excel(golden_file)
|
| 19 |
df1 = df1.drop([0, 1, 2, 3])
|
|
@@ -32,11 +31,18 @@ def process_files(golden_file, test_file):
|
|
| 32 |
col: {"LL": limits_df1.iloc[0][col], "UL": limits_df1.iloc[1][col]}
|
| 33 |
for col in limits_df1.columns
|
| 34 |
}
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def generate_plot(col):
|
| 39 |
-
"""Generate a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
plt.figure(figsize=(6, 4))
|
| 41 |
|
| 42 |
x1 = np.arange(1, len(df1[col]) + 1)
|
|
@@ -69,8 +75,6 @@ def generate_plot(col):
|
|
| 69 |
|
| 70 |
@app.route("/", methods=["GET", "POST"])
|
| 71 |
def index():
|
| 72 |
-
global df1, df2, limits, cols_to_plot
|
| 73 |
-
|
| 74 |
if request.method == "POST":
|
| 75 |
golden_file = request.files.get("golden_file")
|
| 76 |
test_file = request.files.get("test_file")
|
|
@@ -79,27 +83,31 @@ def index():
|
|
| 79 |
return render_template("index.html", error="Please upload both files.")
|
| 80 |
|
| 81 |
try:
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
# Show the first 3 plots initially
|
| 85 |
-
preview_cols = cols_to_plot[:3]
|
| 86 |
return render_template(
|
| 87 |
"plot.html",
|
| 88 |
-
cols=
|
| 89 |
-
preview_cols=preview_cols
|
| 90 |
)
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
-
return render_template("index.html", error=f"Error: {str(e)}")
|
| 94 |
|
| 95 |
return render_template("index.html")
|
| 96 |
|
| 97 |
|
| 98 |
@app.route("/plot_image/<col>")
|
| 99 |
def plot_image(col):
|
| 100 |
-
"""Return plot
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, send_file
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Store data globally (for demo simplicity — in production, use session or caching)
|
| 11 |
+
data_cache = {"df1": None, "df2": None, "limits": {}, "cols": []}
|
| 12 |
+
|
| 13 |
|
| 14 |
def process_files(golden_file, test_file):
|
| 15 |
+
"""Load Excel files and prepare dataframes and limits."""
|
| 16 |
limits_df1 = pd.read_excel(golden_file, nrows=4)
|
| 17 |
df1 = pd.read_excel(golden_file)
|
| 18 |
df1 = df1.drop([0, 1, 2, 3])
|
|
|
|
| 31 |
col: {"LL": limits_df1.iloc[0][col], "UL": limits_df1.iloc[1][col]}
|
| 32 |
for col in limits_df1.columns
|
| 33 |
}
|
| 34 |
+
|
| 35 |
+
# Store in cache for later retrieval
|
| 36 |
+
data_cache["df1"], data_cache["df2"], data_cache["limits"], data_cache["cols"] = df1, df2, limits, cols_to_plot
|
| 37 |
|
| 38 |
|
| 39 |
def generate_plot(col):
|
| 40 |
+
"""Generate and return a PNG plot for a given column."""
|
| 41 |
+
df1, df2, limits = data_cache["df1"], data_cache["df2"], data_cache["limits"]
|
| 42 |
+
|
| 43 |
+
if df1 is None or col not in df1.columns:
|
| 44 |
+
raise ValueError(f"Data not loaded or column '{col}' missing")
|
| 45 |
+
|
| 46 |
plt.figure(figsize=(6, 4))
|
| 47 |
|
| 48 |
x1 = np.arange(1, len(df1[col]) + 1)
|
|
|
|
| 75 |
|
| 76 |
@app.route("/", methods=["GET", "POST"])
|
| 77 |
def index():
|
|
|
|
|
|
|
| 78 |
if request.method == "POST":
|
| 79 |
golden_file = request.files.get("golden_file")
|
| 80 |
test_file = request.files.get("test_file")
|
|
|
|
| 83 |
return render_template("index.html", error="Please upload both files.")
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
process_files(golden_file, test_file)
|
| 87 |
+
|
| 88 |
+
cols = data_cache["cols"]
|
| 89 |
+
preview_cols = cols[:3] if len(cols) >= 3 else cols
|
| 90 |
|
|
|
|
|
|
|
| 91 |
return render_template(
|
| 92 |
"plot.html",
|
| 93 |
+
cols=cols,
|
| 94 |
+
preview_cols=preview_cols
|
| 95 |
)
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
+
return render_template("index.html", error=f"Error processing files: {str(e)}")
|
| 99 |
|
| 100 |
return render_template("index.html")
|
| 101 |
|
| 102 |
|
| 103 |
@app.route("/plot_image/<col>")
|
| 104 |
def plot_image(col):
|
| 105 |
+
"""Return plot image stream for selected column."""
|
| 106 |
+
try:
|
| 107 |
+
buf = generate_plot(col)
|
| 108 |
+
return send_file(buf, mimetype="image/png")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return f"Error generating plot: {str(e)}"
|
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
templates/plot.html
CHANGED
|
@@ -36,10 +36,14 @@
|
|
| 36 |
<h2>📈 IPM Data Comparison</h2>
|
| 37 |
|
| 38 |
<h3>Preview (First few plots):</h3>
|
| 39 |
-
{%
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
<hr>
|
| 45 |
<h3>View Other Columns</h3>
|
|
|
|
| 36 |
<h2>📈 IPM Data Comparison</h2>
|
| 37 |
|
| 38 |
<h3>Preview (First few plots):</h3>
|
| 39 |
+
{% if preview_cols %}
|
| 40 |
+
{% for c in preview_cols %}
|
| 41 |
+
<div class="preview-title">{{ c }}</div>
|
| 42 |
+
<img src="/plot_image/{{ c }}" alt="{{ c }}">
|
| 43 |
+
{% endfor %}
|
| 44 |
+
{% else %}
|
| 45 |
+
<p>No columns found for preview.</p>
|
| 46 |
+
{% endif %}
|
| 47 |
|
| 48 |
<hr>
|
| 49 |
<h3>View Other Columns</h3>
|