Skip to content

Commit 6c85ddd

Browse files
this-joshConengmo
andauthored
Add ability to set font size in plot (#1879)
* Added a function to process font sizes * Add ability to set font size in plot and test it * formatting with black * correct import order * move arg to be the last and added a docstring * Update folium/folium.py Co-authored-by: Frank Anema <[email protected]> * Change to accept em and px made px the default in line with _parse_size. * correct test units * formatting with black * formatting with black * Update tests/test_utilities.py * Update tests/test_utilities.py --------- Co-authored-by: Frank Anema <[email protected]>
1 parent 36f2ff6 commit 6c85ddd

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

Diff for: folium/folium.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TypeBounds,
1818
TypeJsonValue,
1919
_parse_size,
20+
parse_font_size,
2021
parse_options,
2122
temp_html_filepath,
2223
validate_location,
@@ -152,6 +153,9 @@ class Map(JSCSSMixin, MacroElement):
152153
rare environments) even if they're supported.
153154
zoom_control : bool, default True
154155
Display zoom controls on the map.
156+
font_size : int or float or string (default: '1rem')
157+
The font size to use for Leaflet, can either be a number or a
158+
string ending in 'rem', 'em', or 'px'.
155159
**kwargs
156160
Additional keyword arguments are passed to Leaflets Map class:
157161
https://leafletjs.com/reference.html#map
@@ -186,7 +190,7 @@ class Map(JSCSSMixin, MacroElement):
186190
left: {{this.left[0]}}{{this.left[1]}};
187191
top: {{this.top[0]}}{{this.top[1]}};
188192
}
189-
.leaflet-container { font-size: 1rem; }
193+
.leaflet-container { font-size: {{this.font_size}}; }
190194
</style>
191195
{% endmacro %}
192196
@@ -253,6 +257,7 @@ def __init__(
253257
disable_3d: bool = False,
254258
png_enabled: bool = False,
255259
zoom_control: bool = True,
260+
font_size: str = "1rem",
256261
**kwargs: TypeJsonValue,
257262
):
258263
super().__init__()
@@ -276,6 +281,7 @@ def __init__(
276281
self.left = _parse_size(left)
277282
self.top = _parse_size(top)
278283
self.position = position
284+
self.font_size = parse_font_size(font_size)
279285

280286
max_bounds_array = (
281287
[[min_lat, min_lon], [max_lat, max_lon]] if max_bounds else None

Diff for: folium/utilities.py

+10
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,13 @@ def __init__(self, js_code: Union[str, "JsCode"]):
428428
self.js_code: str = js_code.js_code
429429
else:
430430
self.js_code = js_code
431+
432+
433+
def parse_font_size(value: Union[str, int, float]) -> str:
434+
"""Parse a font size value, if number set as px"""
435+
if isinstance(value, (int, float)):
436+
return f"{value}px"
437+
438+
if (value[-3:] != "rem") and (value[-2:] not in ["em", "px"]):
439+
raise ValueError("The font size must be expressed in rem, em, or px.")
440+
return value

Diff for: tests/test_folium.py

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def setup_method(self):
7575
max_zoom=20,
7676
zoom_start=4,
7777
max_bounds=True,
78+
font_size="1.5rem",
7879
attr=attr,
7980
)
8081
self.env = Environment(loader=PackageLoader("folium", "templates"))
@@ -94,6 +95,7 @@ def test_init(self):
9495
assert self.m.top == (0, "%")
9596
assert self.m.global_switches.no_touch is False
9697
assert self.m.global_switches.disable_3d is False
98+
assert self.m.font_size == "1.5rem"
9799
assert self.m.to_dict() == {
98100
"name": "Map",
99101
"id": self.m._id,

Diff for: tests/test_utilities.py

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
get_obj_in_upper_tree,
1313
if_pandas_df_convert_to_numpy,
1414
javascript_identifier_path_to_array_notation,
15+
parse_font_size,
1516
parse_options,
1617
validate_location,
1718
validate_locations,
@@ -230,3 +231,26 @@ def test_js_code_init_js_code():
230231
js_code_2 = JsCode(js_code)
231232
assert isinstance(js_code_2, JsCode)
232233
assert isinstance(js_code_2.js_code, str)
234+
235+
236+
@pytest.mark.parametrize(
237+
"value,expected",
238+
[
239+
(10, "10px"),
240+
(12.5, "12.5px"),
241+
("1rem", "1rem"),
242+
("1em", "1em"),
243+
],
244+
)
245+
def test_parse_font_size_valid(value, expected):
246+
assert parse_font_size(value) == expected
247+
248+
249+
invalid_values = ["1", "1unit"]
250+
expected_errors = "The font size must be expressed in rem, em, or px."
251+
252+
253+
@pytest.mark.parametrize("value,error_message", zip(invalid_values, expected_errors))
254+
def test_parse_font_size_invalid(value, error_message):
255+
with pytest.raises(ValueError, match=error_message):
256+
parse_font_size(value)

0 commit comments

Comments
 (0)