Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 2, 2023 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/4998521f496dd8a88f2d1ba09de654eb to your computer and use it in GitHub Desktop.
Save code-boxx/4998521f496dd8a88f2d1ba09de654eb to your computer and use it in GitHub Desktop.
Python Flask Display CSV In HTML Table

PYTHON DISPLAY CSV IN HTML TABLE

https://code-boxx.com/csv-table-python-flask/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create a templates folder, move S3_csv_table.html inside.
    • Create a virtual environment - virtualenv venv.
    • Activate the virtual environment - venv\scripts\activate (Windows) venv/bin/activate (Mac/Linux)
    • Install Flask - pip install flask
    • Run python S2_server.py to start the server.
  2. Access http://localhost in your browser.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Jo Doe jo@doe.com 465785
Joa Doe joa@doe.com 123456
Job Doe job@doe.com 234567
Joe Doe joe@doe.com 345678
Jog Doe jog@doe.com 578456
Joh Doe joh@doe.com 378945
Joi Doe joi@doe.com 456789
Jon Doe jon@doe.com 987654
Jor Doe jor@doe.com 754642
Joy Doe joy@doe.com 124578
# (A) INIT
# (A1) LOAD MODULES
from flask import Flask, render_template
import csv
# (A2) FLASK SETTINGS + INIT
HOST_NAME = "localhost"
HOST_PORT = 80
app = Flask(__name__)
# app.debug = True
# (B) DEMO - READ CSV & GENERATE HTML TABLE
@app.route("/")
def index():
with open("S1_dummy.csv") as file:
reader = csv.reader(file)
return render_template("S3_csv_table.html", csv=reader)
# (C) START
if __name__ == "__main__":
app.run(HOST_NAME, HOST_PORT)
<!DOCTYPE html>
<html>
<head>
<title>CSV To HTML Table</title>
<style>
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
#demo { border-collapse: collapse; }
#demo tr:nth-child(odd) { background: #f2f2f2; }
#demo td { padding: 10px; }
</style>
</head>
<body>
<table id="demo">
{% for row in csv %}
<tr>
{% for col in row %}
<td>{{ col }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>
md templates
move S3_csv_table.html templates
virtualenv venv
call venv\Scripts\activate
pip install flask
python S2_server.py
mkdir -m 777 templates
mv ./S3_csv_table.html ./templates
virtualenv venv
source "venv/bin/activate"
pip install flask
python S2_server.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment