Reputation: 1658
I'm trying to save Pandas DataFrame to html file with embedded JavaScript code for autorefreshing.
My code is:
import pandas as pd
html_string = """
<html>
<head>
<script>
function timedRefresh(timeoutPeriod)
{
setTimeout("location.reload(true);",timeoutPeriod);
}
window.onload = timedRefresh(5000);
</script>
</head>
<link rel="stylesheet" type="text/css" href="df_style.css"/>
<body>
{table}
</body>
</html>
"""
df1.to_html("/html/calls.html")
with open('/html/calls.html', 'w') as f:
f.write(html_string.format(table=df1.to_html(classes='mystyle')))
But i get error:
KeyError: 'setTimeout("location'
Upvotes: 1
Views: 630
Reputation: 82765
You need to escape the curly brackets of the JS function. Use double brackets {{}}
Ex:
html_string = """
<html>
<head>
<script>
function timedRefresh(timeoutPeriod)
{{
setTimeout("location.reload(true);",timeoutPeriod);
}}
window.onload = timedRefresh(5000);
</script>
</head>
<link rel="stylesheet" type="text/css" href="df_style.css"/>
<body>
{table}
</body>
</html>
"""
Upvotes: 1