Skip to content

Fix numeric keys for dict inputs in tojavascript #2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion folium/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def tojavascript(obj: Union[str, JsCode, dict, list, Element]) -> str:
elif isinstance(obj, dict):
out = ["{\n"]
for key, value in obj.items():
out.append(f' "{camelize(key)}": ')
if isinstance(key, str):
out.append(f' "{camelize(key)}": ')
else:
out.append(f" {key}: ")
out.append(tojavascript(value))
out.append(",\n")
out.append("}")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def test_tojavascript_with_dict():
assert tojavascript(dict_obj) == '{\n "key": "value",\n}'


def test_tojavascript_with_dict_with_mixed_key_types():
dict_obj = {"key": "value", 1: "another value", 3.14: "pi"}
expected = '{\n "key": "value",\n 1: "another value",\n 3.14: "pi",\n}'
assert tojavascript(dict_obj) == expected


def test_tojavascript_with_list():
list_obj = ["value1", "value2"]
assert tojavascript(list_obj) == '[\n"value1",\n"value2",\n]'
Expand Down