I am trying to render a custom html table that is styled with css. I can get my table loaded, but the referenced css file is not used for some reason. Here’s an example of my html code
<html>
<head><title>HTML Pandas Dataframe with CSS</title></head>
<link rel="stylesheet" type="text/css" href="df_style.css"/>
<body>
<table border="1" class="dataframe mystyle">
<thead>
<tr style="text-align: center;">
<th></th>
<th>team</th>
<th>player</th>
<th>week</th>
<th>year</th>
<th>x</th>
<th>y</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td><img src="https://a.espncdn.com/i/teamlogos/nfl/500/kc.png" width="40" style="text-align: center"></td>
<td><img src="https://static.www.nfl.com/image/private/f_auto,q_auto/league/xw0jhb7zeobj4bkxfpwg" width="75" style="text-align: center"></td>
<td>1</td>
<td>2020</td>
<td>15</td>
<td>10</td>
</tr>
</tbody>
</table>
</body>
</html>.
here is how I generated this html using pandas
html_string = """
<html>
<head><title>HTML Pandas Dataframe with CSS</title></head>
<link rel="stylesheet" type="text/css" href="df_style.css"/>
<body>
{table}
</body>
</html>.
"""
html_string.format(
table=df.to_html(
classes="mystyle",
escape=False,
formatters=format_dict,
justify="center",
)
)
and here is my css file, df_style.css
.mystyle {
font-size: 11pt;
font-family: Arial;
border-collapse: collapse;
border: 1px solid rgb(237, 69, 69);
}
and here is how I rendered the table via streamlit
components.html(
html_string.format(
table=c1.to_html(
classes="mystyle",
escape=False,
formatters=format_dict,
justify="center",
)
),
)
but the table just renders with default html table styling rather than referencing my css. This stackoverflow question notes that pandas *.to_html()
“adds dual classes <table class="dataframe mystyle">
which can be referenced in CSS individually, .dataframe {...} .mystyle{...}
, or together .dataframe.mystyle {...}
” but I still can’t get the streamlit to reference my css file when using any of those naming conventions for the css classes in my file. Any ideas? This should be possible considering the example in the documentation references a bootstrap css file.
Edit: Calling the amazing @andfanilo
EDIT again: per this post, I was able to get the css to render by putting it in the static
streamlit folder. Now the question is will the css still render correctly if I just have all my files in a github repo?