I recently discovered that it's quite easy to inject HTML into Jupiter notebook to visualize python objects. That seems particularly useful to me to write a custom interactive plot for pandas dataframes.
The most basic example is:
from IPython.display import HTML
import pandas as pd
HTML( pd.DataFrame([[1,2],[3,4]]).to_html() )
# prints the html representation of a dataframe
What I am trying to accomplish is something similar to what apache zeppelin implements (https://zeppelin.apache.org/, data visualization), where you can pivot columns interactively to explore the data frame.
The first solution I can think of is writing a function get_html that takes the pandas df as input in a json format and visualizes it via d3, pseudocode:
def get_html(json_data):
big_html = \
f"""
<html>
<script>
// inject {json_data} into the js library of your choice
<script>
</html>
"""
return big_html
HTML( get_html(df.to_json(orient="records")) )
This seems suboptimal for a number of reasons, for instance
js library, instead of embedding js that way)However, I cannot figure out how to make this work better.
An idea could be dumping the df onto a json and then re-reading from the disk, but I don't know how to do it.