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

allow multiple values in query strings #73

Merged
merged 3 commits into from
Jan 26, 2022

Conversation

AnnMarieW
Copy link
Collaborator

This pull request allows for multiple values in query strings to be passed to the layout function.

For example, this URL has 2 values for indi:

http://127.0.0.1:8050/chart?com=AAA&indi=sma-50&indi=sma-200

The following is now passed to the layout function. Note that the value for indi is a list

{'com': 'AAA', 'indi': ['sma-50', 'sma-200']}

Previously, only the first value was included:

{'com': 'AAA', 'indi': 'sma-50'}

try:
first = json.loads(first)
v = json.loads(v)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the use case for json loading the value here? And in particular, do we need to json.loads each item, if we have a list at this point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@chriddyp can you answer this question about the use-case for json.loads?

Copy link
Member

Choose a reason for hiding this comment

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

JSON parsing - to bring number strings into numbers and allow human readable booleans (true, false)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, @chriddyp I don't understand your answer - could you elaborate?

Copy link
Member

Choose a reason for hiding this comment

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

Example: ?weekends=false&start=5. If we don't JSON parse, then the dev would get literally the strings 'false' and '5'. By JSON parsing, the dev will get False (a Python boolean) and 5 (a number)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@alexcjohnson should we also use this with the path variables for consistency?

If so, should the default path when using a path_template be "/dasboard/null" rather than "/dashboard/none" ?

Copy link
Member

Choose a reason for hiding this comment

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

"False", "True" and "None" remain as strings.

Oh true, good point. That feels a bit confusing, I could imagine that we convert those to the Python types as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

After giving this a little more thought, I think it might be better to keep data as strings. For example, not all string numbers should be converted to int or float. What if the number was part of a 3 digit string id? "?id=010" would be converted as a string {"id": "010"} but "?id=100" would be converted as an int {"id": 100} .

Plus there are other keywords that would convert that people might not expect -- such as "NaN" and "Infinity"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, I'm starting to think the same - with all the questions about JS vs Python keywords, and now @AnnMarieW's point about numeric strings that aren't numbers, automatically converting just seems too ambiguous and confusing.

For path variables we could adopt Flask's convention of <converter:name>, but unless we can think of a good place to specify similar converters for query parameters I'm leaning toward leaving them as strings.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@chriddyp Is this good to go now? I removed the json.loads.

Copy link
Member

@chriddyp chriddyp left a comment

Choose a reason for hiding this comment

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

💃🏼

@AnnMarieW
Copy link
Collaborator Author

Yippee - Thanks!

@AnnMarieW AnnMarieW merged commit c8d92cc into plotly:main Jan 26, 2022
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.

3 participants