tabulate has options headers and showindex
table = tb.tabulate(data, tablefmt='html', headers=df.columns, showindex=True)
st.markdown(table, unsafe_allow_html=True)

Source code: tabulate
You can also add CSS to change it
table = tb.tabulate(data, tablefmt='html', headers=df.columns, showindex=True)
st.write('<style>table th {font-size: 30px; color: red} table tr:nth-child(odd) td {background-color: #eee}</style>', unsafe_allow_html=True)
st.markdown(table, unsafe_allow_html=True)

But you may also use DataFrame to generate HTML without tabulate.
And it may use df.style with many functions to format every cell.
This example changes background for maximal and minimal value in columns A and B
df = pd.DataFrame(data)
def highlight_max(x, props):
return np.where(x == np.nanmax(x.to_numpy()), props, None)
def highlight_min(x, props):
return np.where(x == np.nanmin(x.to_numpy()), props, None)
df = df.style \
.apply(highlight_max, props='background-color: #fed', subset=['A','B'], axis=0) \
.apply(highlight_min, props='background-color: #afa', subset=['A','B'], axis=0)
html = df.to_html()
st.write(html, unsafe_allow_html=True)

Full code:
import tabulate as tb
import streamlit as st
import pandas as pd
import numpy as np
np.random.seed(42)
data = {
'A': np.random.rand(5),
'B': np.random.randint(1, 100, size=5),
'C': np.random.choice(['X', 'Y', 'Z'], size=5)
}
df = pd.DataFrame(data)
def highlight_max(x, props):
return np.where(x == np.nanmax(x.to_numpy()), props, None)
def highlight_min(x, props):
return np.where(x == np.nanmin(x.to_numpy()), props, None)
df = df.style \
.apply(highlight_max, props='background-color: #fed', subset=['A','B'], axis=0) \
.apply(highlight_min, props='background-color: #afa', subset=['A','B'], axis=0)
html = df.to_html()
st.write(html, unsafe_allow_html=True)
table = tb.tabulate(data, tablefmt='html', headers=df.columns, showindex=True)
st.write('<style>table th {font-size: 30px; color: red} table tr:nth-child(odd) td {background-color: #eee}</style>', unsafe_allow_html=True)
st.markdown(table, unsafe_allow_html=True)
Answer from furas on Stack OverflowPython - HTML - Send HTML Table with CSS Style - Stack Overflow
HTML format with grid
smtplib - Printing mutiple HTML tables using tabulate in python - Stack Overflow
Using tabulate prints string instead of html file
Videos
» pip install tabulate
Hey guys,
I am trying to work through building a webapp for a personal project and mainly just to learn how to do it. I have functions set up to webscrape some data, which is stored in a SQLite database. I pull in the data from the databases fine, which is given as a list of lists. From there I am trying to use "tabulate" which works fine in my jupyter notebook to form a table as a test, and when I print the output it gives me my html formatted code.
The issue is when I try to display it in my webapp by trying:
{% extends "base.html" %} {% block title %}Home{% endblock %} {% block content%}
{% print(var1) %}
{% endblock %}
Where var1 is:
conn = sqlite3.connect(r"directorystring\database.db")
cur = conn.cursor()var = cur.execute("SELECT * from Table1").fetchall()
var1= tabulate(var, tablefmt='html')
I've also tried {{ var1 }} instead of {% print(var1) %} and every other combination I can think of but all it does is either display a blank webpage or the actual string of html code <table> <tbody> <tr><td>Entry 1</td><td style="text-align: right;">... etc
Firstly, is using tabulate a good way of going about it, and is there a better/easier way to post a data table from sqlite to html? Secondly does anyone have any idea what I'm doing wrong?! I hope I've included the correct parts of the code.
Thanks!
EDIT: sorry for the formatting i've tried several times to get it working properly, i've even copied from the markdown wiki and it still won't work!