Last active
January 4, 2023 10:29
-
-
Save joelhoro/67d327624ce254f4a4abce0a991124d2 to your computer and use it in GitHub Desktop.
Pandas dataframe with bootstrap styling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[1]: | |
data = [ | |
{"name": "Argentina", "year": 2005, "continent": "South America", "form": "Republic", "gdp": 181.357, "oil": 1.545, "balance": 4.699}, | |
{"name": "Argentina", "year": 2006, "continent": "South America", "form": "Republic", "gdp": 212.507, "oil": 1.732, "balance": 7.167}, | |
{"name": "Australia", "year": 2012, "continent": "Australasia", "form": "Constitutional monarchy", "gdp": 1542.055, "oil": 22.596, "balance": -62.969}, | |
{"name": "Australia", "year": 2013, "continent": "Australasia", "form": "Constitutional monarchy", "gdp": 1598.069, "oil": 23.650, "balance": -87.944}, | |
{"name": "Austria", "year": 2005, "continent": "Europe", "form": "Republic", "gdp": 305.513, "oil": 7.600, "balance": 6.615}, | |
{"name": "Austria", "year": 2006, "continent": "Europe", "form": "Republic", "gdp": 325.256, "oil": 9.344, "balance": 9.112}, | |
{"name": "Austria", "year": 2009, "continent": "Europe", "form": "Republic", "gdp": 384.622, "oil": 8.475, "balance": 10.428}, | |
{"name": "Belarus", "year": 2009, "continent": "Europe", "form": "Republic", "gdp": 49.209, "oil": 8.386, "balance": -6.178}, | |
{"name": "Belarus", "year": 2010, "continent": "Europe", "form": "Republic", "gdp": 55.221, "oil": 7.686, "balance": -8.278}, | |
{"name": "Belarus", "year": 2013, "continent": "Europe", "form": "Republic", "gdp": 58.933, "oil": 11.434, "balance": -3.419}, | |
{"name": "Belgium", "year": 2005, "continent": "Europe", "form": "Constitutional monarchy", "gdp": 378.006, "oil": 24.956, "balance": 7.465}, | |
{"name": "Brazil", "year": 2011, "continent": "South America", "form": "Republic", "gdp": 2492.907, "oil": 37.498, "balance": -52.480}, | |
{"name": "Brazil", "year": 2012, "continent": "South America", "form": "Republic", "gdp": 2425.052, "oil": 37.763, "balance": -62.341}, | |
] | |
# In[2]: | |
import pandas as pd | |
df = pd.DataFrame(data) | |
# In[10]: | |
template = """" | |
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> | |
<style> | |
th { | |
background: #2d2d71; | |
color: white; | |
text-align: left; | |
} | |
</style> | |
<body> | |
%s | |
</body> | |
""" | |
# check https://getbootstrap.com/docs/4.0/content/tables/ | |
classes = 'table table-striped table-bordered table-hover table-sm' | |
html= template % df.to_html(classes=classes) | |
file_name = 'test.html' | |
with open(file_name,'w') as f: | |
f.write(html) | |
# In[ ]: | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, helped me