-
Notifications
You must be signed in to change notification settings - Fork 39
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
Handle Variables in the URL #61
Conversation
dash_labs/plugins/pages.py
Outdated
if template_segments[i].startswith("<"): | ||
# removes the "<" ">" around the variable key | ||
variable_key = template_segments[i][1:-1] | ||
path_segments[i] = None if path_segments[i] == "None" else path_segments[i] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This "None"
-> None
conversion worries me - what if you really did want to pass "None"
as the parameter? Can we just leave it as "None"
? I know up above we use "None"
in the path when registering the page but I'm not sure this is a good idea either - for example, how is that going to work in the title, description, and image?
dash-labs/dash_labs/plugins/pages.py
Lines 324 to 332 in f182bc0
path_to_title = { | |
page["path"]: page["title"] for page in dash.page_registry.values() | |
} | |
path_to_description = { | |
page["path"]: page["description"] for page in dash.page_registry.values() | |
} | |
path_to_image = { | |
page["path"]: page["image"] for page in dash.page_registry.values() | |
} |
I suspect the better way to handle this will be to take the path-to-layout finder and turn it into a path-to-page finder, then use that both here and in interpolate_index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I could imagine users wanting title, description, and image to each potentially be a function of the path variables... But for now we should just ensure they work correctly as constant values for each registered page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dropped the "None" => None
conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I could imagine users wanting title, description, and image to each potentially be a function of the path variables... But for now we should just ensure they work correctly as constant values for each registered page.
I think one way to do this is to create a dash.path_registry
. Initially it could be created from the data in dash.page_registry
. Then people could add other path data to it. However, is this worthwhile? Would this be a popular feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect the better way to handle this will be to take the path-to-layout finder and turn it into a path-to-page finder, then use that both here and in interpolate_index.
Good suggestion - I added a path_to_page
function in this commit
Hey @alexcjohnson This is ready for another review. I moved tests to #66 so it doesn't hold up things that are finished, such as:
|
Co-authored-by: Chris Parmer <[email protected]>
Co-authored-by: Alex Johnson <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Stricter pattern checking https://github.com/plotly/dash-labs/pull/61/files#r778960479 would be nice but we could do that later as well.
💃
Co-authored-by: Alex Johnson <[email protected]>
… into path-variables
…variables resolve conflicts black
dash_labs/plugins/pages.py
Outdated
def _validate_template(template): | ||
template_segments = template.split("/") | ||
for s in template_segments: | ||
if ("<" or ">") in s and not (s.startswith("<") and s.endswith(">")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
("<" or ">") in s
-> I think you mean ("<" in s or ">" in s)
Also need to check that <
and >
are NOT in s[1: -1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also need to check that < and > are NOT in s[1: -1]
Just to summarize our discussion: Since the variables are passed to the layout in a dict as strings, there should not be a restriction about including <
or >
. However, the most common use case is that the keys are Python variables, so now there is a warning message if the variable is not a valid Python variable name.
added warning message if path variables are not valid Python variable names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright I think we have a winner! 💃
Closes #45
This is a new feature for handing variables in the URL.
Creates a new parameter
path_template
indash.page_registry
path_template
:Add variables to a URL by marking sections with <variable_name>. The layout function
then receives the <variable_name> as a keyword argument.
e.g. path_template= "/asset/<asset_id>"
path = "/assets/a100"
layout will receive
{"asset_id":"a100"}
If no
path
is supplied when callingdash.register_page()
then it will be inferred from thepath_template
. Variables will default to"none"
.e.g. "/asset/<asset_id>" => "/asset/none"