Process JS code separately [draft] #1856
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Testing out how we can use the concept of the new
JsCode
class, which was added in #1848. Idea is that we no longer have to manually put Javascript code in templates.Problem before was that a Javascript-function-as-Python-string would get transformed into a Javascript string. Because of that we had to put those functions into the template manually.
Now we have a
JsCode
class that we can use to mark strings as Javascript code. When putting stuff in templates, we can check for that class and prevent adding string quotes.I looked into using a custom JSON encoder with the
json
module, but that turns out to be awkward, because what we're producing is not actually valid JSON.So I've opted to make a custom filter like the
tojson
filter we use a lot, but now allowing for Javascript code. To register this filter I had to subclassTemplate
.Using it means:
Template
class fromfolium.template
JsCode.optional_create()
parse_options
.{{ options|tojavascript }}
filter in the template.I've now applied this new workflow on the
LayerControl
,MousePosition
andRealtime
classes.Some smaller included changes:
JsCode
a subclass ofstr
, that makes it easier to put it in a template without additional needed code.JsCode
, I figured that's more user-friendly then requiringJsCode
.MousePosition
example with the custom formatter no longer works with the trailing;
character, so that's a breaking change.If this seems like a good change I'll add tests as well.
Question is whether the added complexity of the new code weighs up to the more straightforward way in working with Javascript code.