Repository navigation
Serialize numpy values in template options - #2366
Open
KhadeerBasha1232 wants to merge 1 commit into
Open
KhadeerBasha1232 wants to merge 1 commit into
KhadeerBasha1232 wants to merge 1 commit into
Conversation
Options are written into the page with json.dumps, through the tojavascript filter and Jinja's tojson. json can't serialize numpy integers, booleans or arrays, so passing one as an option made rendering fail with "TypeError: Object of type int64 is not JSON serializable". That's common when values come from a DataFrame, for example CircleMarker(radius=row.count) or fit_bounds(df[[...]].values). Give both serializers a default that converts numpy scalars with .item() and arrays with .tolist(). Other unsupported types still raise the same TypeError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KhadeerBasha1232
temporarily deployed
to
pypi
November 20, 2026 04:28 — with
GitHub Actions
Inactive
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Serialize numpy values in template options
Passing a numpy integer or array as an option makes rendering fail:
Options are written into the page with
json.dumps, throughtojavascriptand Jinja'stojson.jsoncan't serializenp.int64,np.bool_ornp.ndarray. (np.float64works only because it subclassesfloat.) Values like these are what you get from a DataFrame, e.g.radius=row.count,max_zoom=df.zoom.max()orfit_bounds(df[["lat", "lon"]].values).This was reported in #1208. It was closed then because casting every argument by hand would need a lot of code. Since #1912, all options go through the serializers in
folium/template.py, so this can now be fixed in one place. #2244 fixed theHeatMapdata case on its own.Before / after
CircleMarker(..., radius=np.int64(5))TypeError"radius": 5PolyLine(..., weight=np.int64(3))TypeErrorHeatMap(..., radius=np.int64(10))TypeErrorTileLayer(max_zoom=np.int64(18))TypeError"maxZoom": 18Popup("x", max_width=np.int64(300))TypeError"maxWidth": 300MiniMap(zoom_level_offset=np.int64(-5))TypeErrorDivIcon(icon_size=np.array([20, 20]))TypeError(ndarray)"iconSize": [20, 20]m.fit_bounds(np.array([[45, 3], [46, 4]]))TypeError(ndarray)fitBounds([[45.0, 3.0], [46.0, 4.0]], ...)m.fit_bounds(bounds, max_zoom=np.int64(5))TypeError"maxZoom": 5With this change, the example above renders. I opened it in a browser: the three circles have radii 3, 8 and 15,
fit_boundszooms to them, and there are no console errors.Change
folium/template.pygets a_json_defaultfunction, whichjson.dumpscalls for objects it can't serialize:np.generic) are converted with.item().tolist()TypeErroras beforeIt is used by
_to_escaped_json(thetojavascriptfilter) and set as thedefaultin the environment'sjson.dumps_kwargspolicy, so thetojsonfilter handles numpy values too. Output for values that already worked doesn't change.Tests
tests/test_template.py: numpy scalars, arrays and nested values through_to_escaped_json,tojavascriptand|tojson, plus a check that unsupported types still raiseTypeError.tests/test_map.py:test_numpy_option_valuesrendersCircleMarker,TileLayer,Popup,DivIconandfit_boundswith numpy options.All 14 new numpy tests fail without the change (the
TypeErrortest passes either way, as it should). The rest of the test suite and ruff pass.