Skip to content

Allow JsCode to reference other map objects - #2333

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:jscode-template-params
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:jscode-template-params

Conversation

@KhadeerBasha1232

Copy link
Copy Markdown
Contributor

Add template parameters to JsCode

Closes #2121

JsCode now accepts keyword arguments that fill %(name)s placeholders in the code, as proposed in the issue:

handler = JsCode("""
    var treeControl = %(tree)s
    // do something with the control
""", tree=tree_control)

How values are inserted

  • A folium/branca Element is replaced by its Javascript variable name (get_name()).
  • A JsCode object is replaced by its code, so snippets can be nested.
  • Any other value is formatted with the % operator as usual.

Details

  • Placeholders are filled in when the code is rendered, not when JsCode is created. So an element's name is read at render time.
  • Without parameters, the code is used as is. Existing code with a literal % (e.g. a % b) keeps working. When parameters are given, a literal % must be written as %%, as with normal % formatting.
  • js_code is still a readable (and settable) attribute, so templates that use {{ this.handler.js_code }} and tojavascript work unchanged.
  • JsCode(other_js_code, **params) keeps the other object's parameters and merges in the new ones.

Changes

  • folium/utilities.py: parameters and docstring for JsCode.
  • tests/test_utilities.py: tests for elements, nested JsCode, plain values, %% escaping, no-parameter passthrough, merging, render-time resolution, and rendering inside a map event handler.
  • docs/advanced_guide/customize_javascript_and_css.md: new section "Referring to other map objects in JsCode".

JsCode now accepts keyword arguments that fill %(name)s placeholders.
Elements are replaced by their Javascript variable name, JsCode objects
by their code, and other values are formatted as usual. Placeholders are
filled in at render time; code without parameters is used as is.

Closes python-visualization#2121

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
pypi — de523165 Deployed Nov 4, 2026 by KhadeerBasha1232 via packages #2043
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add template parameters to JsCode objects

1 participant