Skip to content

Instantly share code, notes, and snippets.

@wbhinton
Created December 3, 2019 16:08
Show Gist options
  • Save wbhinton/f14cda07b5bb207fe1b607bdb7c401dd to your computer and use it in GitHub Desktop.
Save wbhinton/f14cda07b5bb207fe1b607bdb7c401dd to your computer and use it in GitHub Desktop.
Export Pandas DataFrame as a HTML Table
# Set the HTML Header with Bootstrap4 CSS for table formatting
html_string = """
<html>
<head><title>HTML Pandas Dataframe with CSS</title></head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<body>
{table}
</body>
</html>
"""
# OUTPUT AN HTML FILE
htmlfile = str(ID) + "-table.html"
with open(htmlfile, "w") as f:
f.write(
html_string.format(
table=df6.to_html(
classes="table table-hover table-striped",
index=False,
show_dimensions=True,
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment