Skip to content

Instantly share code, notes, and snippets.

@s-rohith
Created September 22, 2023 16:21
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 s-rohith/ba34456ba88adbc398bcf10ddc9cc273 to your computer and use it in GitHub Desktop.
Save s-rohith/ba34456ba88adbc398bcf10ddc9cc273 to your computer and use it in GitHub Desktop.
Flask app to print pretty Table output
from flask import Flask, render_template
from prettytable import PrettyTable
app = Flask(__name__)
@app.route('/')
def index():
# Create a PrettyTable instance
table = PrettyTable()
# Define table columns
table.field_names = ["Name", "Age", "Country"]
# Add data rows
table.add_row(["Alice", 25, "USA"])
table.add_row(["Bob", 30, "Canada"])
table.add_row(["Charlie", 22, "UK"])
# Set table alignment
table.align = "l" # Left-align all columns
# Generate an HTML representation of the table
html_table = table.get_html_string(classes=["table", "table-bordered"])
# Create an HTML template with the table
template = f"""
<!DOCTYPE html>
<html>
<head>
<title>Pretty Table Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Pretty Table Example</h1>
{html_table}
</div>
</body>
</html>
"""
return template
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment