Last active
January 15, 2021 11:46
-
-
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
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
| 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