Is there any more efficient method of editing javascript using python like with native function like push to edit the text? I currently do this:
output = f"var {newArraySTR()} = ['templateARRAY']"
for i in functions:
output.replace("'templateARRAY'",f"'{i}','tepmlateARRAY'")
And it's proven much harder to cleanly modify the javascript.
CodePudding user response:
In this specific example, I would not rebuild the array that way. I would use json.dumps to just give me something more js friendly.
I would start with something like:
import json
functions = ["one", "two"]
variable_name = "foo"
variable_value = json.dumps(functions)
js_output = f'''
var {variable_name} = {variable_value};
{variable_name}.forEach(item => {{
console.log(item);
}});
'''
print(js_output)
Giving:
var foo = ["one", "two"];
foo.forEach(item => {
console.log(item);
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
I might also look into a more formal template facility like jinja if this did not work for you.