69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
from flask import Flask, request, Response
|
|
import pyodbc
|
|
import pandas as pd
|
|
from collections import defaultdict
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
CONN = False
|
|
STYLE = """
|
|
<style>
|
|
.my-table {
|
|
border-collapse: collapse;
|
|
width: 100%;
|
|
}
|
|
|
|
.my-table th, .my-table td {
|
|
border: 1px solid #ccc;
|
|
padding: 6px;
|
|
text-align: right;
|
|
vertical-align: top;
|
|
}
|
|
|
|
.my-table th {
|
|
background: #333;
|
|
color: white;
|
|
}
|
|
</style>
|
|
"""
|
|
@app.route("/")
|
|
def tables():
|
|
cursor = CONN.cursor()
|
|
tables = []
|
|
for row in cursor.tables():
|
|
if row.table_type == "TABLE":
|
|
tables.append(row.table_name)
|
|
html = "<h1>Tables</h1><ul>"
|
|
for t in tables:
|
|
html += f'<li><a href="/table/{t}">{t}</a></li>'
|
|
html += "</ul>"
|
|
return html
|
|
|
|
@app.route("/table/<name>")
|
|
def table(name):
|
|
f = request.args.get("f")
|
|
v = request.args.get("v")
|
|
query = f"SELECT * FROM [{name}]"
|
|
if f and v:
|
|
query += f"WHERE `{f}` = ?"
|
|
df = pd.read_sql(query, CONN, params=[v])
|
|
else:
|
|
df = pd.read_sql(query, CONN)
|
|
def gen():
|
|
yield STYLE
|
|
yield "<html><body><table border='1' class='my-table'>"
|
|
yield "<tr>" + "".join(
|
|
f"<th>{c}</th>" for c in df.columns
|
|
) + "</tr>"
|
|
for row in df.itertuples(index=False):
|
|
yield "<tr>" + "".join(
|
|
f"<td>{v}</td>" for v in row
|
|
) + "</tr>"
|
|
yield "</table></body></html>"
|
|
return Response(gen(), mimetype="text/html")
|
|
|
|
if len(sys.argv) > 1:
|
|
CONN = pyodbc.connect(f"{sys.argv[1]}")
|
|
app.run(host="0.0.0.0", port=5000, debug=True, use_reloader=False)
|
|
else:
|
|
print("You need to supply a pyodbc connection string")
|