import os
from flask import Flask, render_template_string, request
import pandas as pd
EXPORTS_DIR = "exports"
PORT = 8899
app = Flask(__name__)
TEMPLATE = """
CSV Viewer
📊 CSV Viewer
{% if files %}
{% else %}
Brak plików w katalogu {{ dir }}
.
{% endif %}
{% if table %}
Plik: {{ filename }}
{{ table | safe }}
{% endif %}
"""
@app.route("/")
def index():
files = [f for f in os.listdir(EXPORTS_DIR) if f.endswith(".csv")]
return render_template_string(TEMPLATE, files=files, table=None, filename=None, dir=EXPORTS_DIR)
@app.route("/view")
def view_file():
filename = request.args.get("file")
if not filename or not filename.endswith(".csv"):
return "Niepoprawny plik.", 400
path = os.path.join(EXPORTS_DIR, filename)
if not os.path.exists(path):
return "Plik nie istnieje.", 404
try:
df = pd.read_csv(path)
html_table = df.to_html(classes="table table-striped table-bordered display", index=False, table_id="csv-table")
files = [f for f in os.listdir(EXPORTS_DIR) if f.endswith(".csv")]
return render_template_string(TEMPLATE, files=files, table=html_table, filename=filename, dir=EXPORTS_DIR)
except Exception as e:
return f"Błąd odczytu CSV: {e}", 500
if __name__ == "__main__":
os.makedirs(EXPORTS_DIR, exist_ok=True)
app.run(host="0.0.0.0", port=PORT)