Skip to content
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

More flexible _parse_size #125

Merged
merged 2 commits into from
Dec 21, 2022

Conversation

Conengmo
Copy link
Member

@Conengmo Conengmo commented Nov 30, 2022

I wanted to pass a value in pixels to the width and height parameters of Map, but found I couldn't. That seemed weird to me. Turns out there are two issues:

  • _parse_size only allows an integer, which it interprets as pixels, or something like '80 %'. It doesn't allow '800 px', which I think it should.
  • branca and folium both have the same nearly identical utility functions, such as _parse_size.

Lengths in CSS can have many units other than '%' or 'px': https://developer.mozilla.org/en-US/docs/Web/CSS/length. I think we should trust our users to know what unit they want to use. Change _parse_size to allow any unit. Add tests.

Other

In another change in folium I'll propose we'll remove the duplicate utility functions and use the ones from branca instead: python-visualization/folium#1676

Why does _parse_size return the value and the unit separately? It makes our templates more verbose. We could just as well make it convert integers to strings, then returning those. But that's a bigger change.

if isinstance(value, (int, float)):
return float(value), "px"
elif isinstance(value, str):
match = re.fullmatch(r"([\d.]+)\s?(\w{1,5}|%)", value.strip())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a regex expert but testing it with online tools seems to work as expect. The firs t is supposed to be the number and the second one the %, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right! I added a comment to clarify

@Conengmo Conengmo merged commit 9f7aaa1 into python-visualization:main Dec 21, 2022
@Conengmo Conengmo deleted the more-flexible-parse-size branch December 21, 2022 17:25
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.

None yet

2 participants