pelme/htpy

Need to avoid automatically escaping characters

kennethdamica opened this issue · 1 comments

I'm working on a project that involves showing charts, and am running into an issue that htpy is escaping all the quotes in my scripts, which makes the Javascript invalid. Is there a way to avoid escaping trusted strings?

Here's an example:

def include_echarts(self):
        return script(src=static("js/echarts.js"))
    
def make_echarts_bar_chart(self, data: list, labels: list, chartname: str):
    return div[
        div(id=f"{chartname}", style="width: 600px;height:400px;"),
        script(type_="text/javascript")[
            f"""
            var myChart = echarts.init(document.getElementById("{chartname}"));
            var option = {{
                xAxis: {{
                    type: 'category',
                    data: {labels}
                }},
                yAxis: {{
                    type: 'value'
                }},
                series: [{{
                    data: {data},
                    type: 'bar'
                }}]
            }};
            myChart.setOption(option);
        """
        ]   
    ]

When I try to render this example, all the quotes around my input data are changed to their html codes, which are not valid in Javascript. I haven't been able to find a way around this.

Closing this because I answered my own question using another issue. Wrapping my script with Markup did the trick. I did not initially think to try this because it is Javascript, not Markup.