Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 15, 2021 11:46
Show Gist options
  • Select an option

  • Save simonthompson99/b25191ede1a7d1ebc122b68c236a40ae to your computer and use it in GitHub Desktop.

Select an option

Save simonthompson99/b25191ede1a7d1ebc122b68c236a40ae to your computer and use it in GitHub Desktop.
[View Pandas Dataframe in Browser] View pandas df in a datatableesque browser window #python #html
from tempfile import NamedTemporaryFile
import webbrowser
base_html = """
<!doctype html>
<html><head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
</head><body>%s<script type="text/javascript">$(document).ready(function(){$('table').DataTable({
"pageLength": 50
});});</script>
</body></html>
"""
def df_html(df):
"""HTML table with pagination and other goodies"""
df_html = df.to_html()
return base_html % df_html
def df_window(df):
"""Open dataframe in browser window using a temporary file"""
with NamedTemporaryFile(delete=False, mode='w+', suffix='.html') as f:
f.write(df_html(df))
print(f.name)
webbrowser.get(using='chrome').open('file://' + f.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment