I am python noob, but I am trying to render a table generated with the python code below inside of an R Markdown chunk. THe python code outputs a nice formatted table when run inside of Jupyter but I can't seem to replcate that inside of a Markdown document.
{python, engine.path = '/usr/bin/python3'}
import pandas as pd
import numpy as np
df1 = pd.read_csv(r'./data/crest_results_table.txt', sep='\t')
df2 = pd.read_csv(r'./data/crest_formats_table.txt', sep='\t')
results_table = df1.pivot_table(values=['Result'],index=['Anlys_Mthd','CAS','AnalTParam','RDCSRS','NRDCSRS','IGWSRS'],columns=['SampNum','LabID','SampDate'],aggfunc=np.max)
formats_table = df2.pivot_table(values=['Result'],index=['Anlys_Mthd','CAS','AnalTParam','RDCSRS','NRDCSRS','IGWSRS'],columns=['SampNum','LabID','SampDate'],aggfunc=np.max)
def color_cells(s):
if s == -1:
return 'color:{0}; background-color: white; font-weight:bold; font-style:italic; font-size:small'.format('black')
elif s == -2:
return 'color:{0}; background-color: orange; font-weight:bold; font-style:italic; font-size:small'.format('black')
elif s == -3:
return 'color:{0}; background-color: yellow; font-weight:bold; font-size:small'.format('black')
elif s == -4:
return 'color:{0}; background-color: beige; font-weight:bold; font-size:small'.format('black')
else:
return 'color:{0}; font-size:small'.format('grey')
t = results_table.style.apply(lambda x: formats_table.applymap(color_cells), axis=None)
ht = t.render()
I've tried to then use the render() method to just save it as html, and then inside of an R chunk save or print it
{r}
htmltools::save_html(py$ht, "table.html")
htmltools::html_print(py$ht)
However, when I view the saved html file it doesn't render correctly even in the browser.
The pandas docs say something about 'wrapping it in an IPython.display.HTML' but not knowing any python I'm not sure what this means.
Ideally I would like the markdown chunk to render the same table formatted as jupyter does with the exact same code.
Thanks