diff --git a/README.md b/README.md index e69de29..ab3460f 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,3 @@ +# How to +Run exposedb.py and supply a pyodbc connection string +Example: `python exposedb.py "DSN=MyDSN"` diff --git a/exposedb.py b/exposedb.py new file mode 100644 index 0000000..de282c3 --- /dev/null +++ b/exposedb.py @@ -0,0 +1,69 @@ +from flask import Flask, request, Response +import pyodbc +import pandas as pd +from collections import defaultdict +import os + +app = Flask(__name__) +CONN = False +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 = "

Tables

" + return html + +@app.route("/table/") +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 "" + yield "" + "".join( + f"" for c in df.columns + ) + "" + for row in df.itertuples(index=False): + yield "" + "".join( + f"" for v in row + ) + "" + yield "
{c}
{v}
" + 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") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..abef080 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +pandas +pyodbc