MCPcopy Index your code
hub / github.com/plotly/dash / strip_relative_path

Function strip_relative_path

dash/_get_paths.py:91–138  ·  view source on GitHub ↗

Return a path with `requests_pathname_prefix` and leading and trailing slashes stripped from it. Also, if None is passed in, None is returned. Use this function with `get_relative_path` in callbacks that deal with `dcc.Location` `pathname` routing. That is, your usage may look l

(path)

Source from the content-addressed store, hash-verified

89
90
91def strip_relative_path(path):
92 """
93 Return a path with `requests_pathname_prefix` and leading and trailing
94 slashes stripped from it. Also, if None is passed in, None is returned.
95 Use this function with `get_relative_path` in callbacks that deal
96 with `dcc.Location` `pathname` routing.
97 That is, your usage may look like:
98 ```
99 app.layout = html.Div([
100 dcc.Location(id='url'),
101 html.Div(id='content')
102 ])
103 @dash.callback(Output('content', 'children'), [Input('url', 'pathname')])
104 def display_content(path):
105 page_name = dash.strip_relative_path(path)
106 if not page_name: # None or ''
107 return html.Div([
108 dcc.Link(href=dash.get_relative_path('/page-1')),
109 dcc.Link(href=dash.get_relative_path('/page-2')),
110 ])
111 elif page_name == 'page-1':
112 return chapters.page_1
113 if page_name == "page-2":
114 return chapters.page_2
115 ```
116 Note that `chapters.page_1` will be served if the user visits `/page-1`
117 _or_ `/page-1/` since `strip_relative_path` removes the trailing slash.
118
119 Also note that `strip_relative_path` is compatible with
120 `get_relative_path` in environments where `requests_pathname_prefix` set.
121 In some deployment environments, like Dash Enterprise,
122 `requests_pathname_prefix` is set to the application name, e.g. `my-dash-app`.
123 When working locally, `requests_pathname_prefix` might be unset and
124 so a relative URL like `/page-2` can just be `/page-2`.
125 However, when the app is deployed to a URL like `/my-dash-app`, then
126 `dash.get_relative_path('/page-2')` will return `/my-dash-app/page-2`
127
128 The `pathname` property of `dcc.Location` will return '`/my-dash-app/page-2`'
129 to the callback.
130 In this case, `dash.strip_relative_path('/my-dash-app/page-2')`
131 will return `'page-2'`
132
133 For nested URLs, slashes are still included:
134 `dash.strip_relative_path('/page-1/sub-page-1/')` will return
135 `page-1/sub-page-1`
136 ```
137 """
138 return app_strip_relative_path(CONFIG.requests_pathname_prefix, path)
139
140
141def app_strip_relative_path(requests_pathname, path):

Callers 1

test_strip_relative_pathFunction · 0.90

Calls 1

app_strip_relative_pathFunction · 0.85

Tested by 1

test_strip_relative_pathFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…