I am working on a dashboard for some monthly metrics on my team. I need to change the font color on a pn object (HTML or Dataframe) depending on their value.
For what I have been able to investigate, the recommended way to do this is with JS.
I have tried to initialize the external js file but it is not working as the font color is not changing.
Here is an example
#imports
import panel as pn
#js files association. The path is a local path on the server
pn.extension(js_files = {‘$’ : ‘/data/coe_dashboard/styles/table_style.js’})
#pn object creation
html_pane = pn.pane.HTML(“”"
This is an Example Code
January
February
March
15
10
8
2
3
34
""")
html_pane
Also here is the content of the js file
td_array = document.getElementsByTagName(“td”);
for (i = 0; i < td_array.length; i++){
if (td_array[i].textContent > 10){
td_array[i].style.color = “red”;
};
};
The problem with you script is that it is included and run at the top of your document. I.e. before the table is in the document. Thus it fails. Something like the below works.
import panel as pn
html_pane = pn.pane.HTML("""
<h1>This is an Example Code</h1>
<table>
<tbody>
<tr>
<th>January</th>
<th>February</th>
<th>March</th>
</tr>
<tr>
<td>15</td>
<td>10</td>
<td>8</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>34</td>
</tr>
</tbody>
</table>
""")
js_pane = pn.pane.HTML("""
<script>
td_array = document.getElementsByTagName("td");
for (i = 0; i < td_array.length; i++){
if (td_array[i].textContent > 10){
td_array[i].style.color = "red";
};
};
</script>
""", width=0, height=0, margin=0, sizing_mode="fixed")
pn.Column(
html_pane, js_pane
).servable()
Please note that if you update the table dynamically you will have to remove (js_pane.object="") and add the JS again (js_pane.object=JS) after you updated the table(s).
@Marc Thanks a lot for your reply! It was really helpful.
As I am trying to apply a conditional color style based on value, I am going with the JS option.
As you predicted, in a dynamic table it works ok only the first time. I have been trying to understand in which part of my code I have to remove and add the js_pane.object.
I have tried a lot of options but nothing has worked.
I would really appreciate if you can give me some guidance.