I have a couple of tables in a .db file. I’d like to create a simple site with Flask that allows users to select a table out of the database and then export it as a .csv file. All the example I find online are too complex, or are focused on creating User/Login schemas.
I've added this to my routes.py file.
import sqlite3 as sql
import os
import csv
from sqlite3 import Error
conn = sql.connect("LocalEconData.db")
tables = []
def sql_fetch(con):
cursorObj = con.cursor()
cursorObj.execute('SELECT name from sqlite_master where type= "table"')
rows = cursorObj.fetchall()
tables.append(rows)
return tables
sql_fetch(conn)
@app.route("/tables", methods=["GET"])
def dropdown():
return render_template("tables.html", tables=tables)
On my tables.html file I have written
{% extends "layout.html" %} {% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dropdown</title>
</head>
<body>
<select name="table" method="GET" action="/">
<option value="{{tables[0]}}" selected>{{tables[0]}}</option>
{% for table in tables[1:] %}
<option value="{{table}}">{{table}}</option>
{% endfor %}
</select>
</body>
</html>
{% endblock content %}
When I run my app, all I get is this so far.
I would need a Dropdown with TIME_SERIES_CODES and UNEMPLOYMENT_RATE as options, then create a button to save/export as CSV.
Any suggestions?