|
| 1 | +from ._utils import AttributeDict |
| 2 | +from . import exceptions |
| 3 | + |
| 4 | +CONFIG = AttributeDict() |
| 5 | + |
| 6 | + |
| 7 | +def get_asset_url(path): |
| 8 | + return app_get_asset_url(CONFIG, path) |
| 9 | + |
| 10 | + |
| 11 | +def app_get_asset_url(config, path): |
| 12 | + if config.assets_external_path: |
| 13 | + prefix = config.assets_external_path |
| 14 | + else: |
| 15 | + prefix = config.requests_pathname_prefix |
| 16 | + return "/".join( |
| 17 | + [ |
| 18 | + # Only take the first part of the pathname |
| 19 | + prefix.rstrip("/"), |
| 20 | + config.assets_url_path.lstrip("/"), |
| 21 | + path, |
| 22 | + ] |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def get_relative_path(path): |
| 27 | + """ |
| 28 | + Return a path with `requests_pathname_prefix` prefixed before it. |
| 29 | + Use this function when specifying local URL paths that will work |
| 30 | + in environments regardless of what `requests_pathname_prefix` is. |
| 31 | + In some deployment environments, like Dash Enterprise, |
| 32 | + `requests_pathname_prefix` is set to the application name, |
| 33 | + e.g. `my-dash-app`. |
| 34 | + When working locally, `requests_pathname_prefix` might be unset and |
| 35 | + so a relative URL like `/page-2` can just be `/page-2`. |
| 36 | + However, when the app is deployed to a URL like `/my-dash-app`, then |
| 37 | + `app.get_relative_path('/page-2')` will return `/my-dash-app/page-2`. |
| 38 | + This can be used as an alternative to `get_asset_url` as well with |
| 39 | + `app.get_relative_path('/assets/logo.png')` |
| 40 | +
|
| 41 | + Use this function with `app.strip_relative_path` in callbacks that |
| 42 | + deal with `dcc.Location` `pathname` routing. |
| 43 | + That is, your usage may look like: |
| 44 | + ``` |
| 45 | + app.layout = html.Div([ |
| 46 | + dcc.Location(id='url'), |
| 47 | + html.Div(id='content') |
| 48 | + ]) |
| 49 | + @app.callback(Output('content', 'children'), [Input('url', 'pathname')]) |
| 50 | + def display_content(path): |
| 51 | + page_name = app.strip_relative_path(path) |
| 52 | + if not page_name: # None or '' |
| 53 | + return html.Div([ |
| 54 | + dcc.Link(href=app.get_relative_path('/page-1')), |
| 55 | + dcc.Link(href=app.get_relative_path('/page-2')), |
| 56 | + ]) |
| 57 | + elif page_name == 'page-1': |
| 58 | + return chapters.page_1 |
| 59 | + if page_name == "page-2": |
| 60 | + return chapters.page_2 |
| 61 | + ``` |
| 62 | + """ |
| 63 | + return app_get_relative_path(CONFIG.requests_pathname_prefix, path) |
| 64 | + |
| 65 | + |
| 66 | +def app_get_relative_path(requests_pathname, path): |
| 67 | + if requests_pathname == "/" and path == "": |
| 68 | + return "/" |
| 69 | + if requests_pathname != "/" and path == "": |
| 70 | + return requests_pathname |
| 71 | + if not path.startswith("/"): |
| 72 | + raise exceptions.UnsupportedRelativePath( |
| 73 | + """ |
| 74 | + Paths that aren't prefixed with a leading / are not supported. |
| 75 | + You supplied: {} |
| 76 | + """.format( |
| 77 | + path |
| 78 | + ) |
| 79 | + ) |
| 80 | + return "/".join([requests_pathname.rstrip("/"), path.lstrip("/")]) |
| 81 | + |
| 82 | + |
| 83 | +def strip_relative_path(path): |
| 84 | + """ |
| 85 | + Return a path with `requests_pathname_prefix` and leading and trailing |
| 86 | + slashes stripped from it. Also, if None is passed in, None is returned. |
| 87 | + Use this function with `get_relative_path` in callbacks that deal |
| 88 | + with `dcc.Location` `pathname` routing. |
| 89 | + That is, your usage may look like: |
| 90 | + ``` |
| 91 | + app.layout = html.Div([ |
| 92 | + dcc.Location(id='url'), |
| 93 | + html.Div(id='content') |
| 94 | + ]) |
| 95 | + @app.callback(Output('content', 'children'), [Input('url', 'pathname')]) |
| 96 | + def display_content(path): |
| 97 | + page_name = app.strip_relative_path(path) |
| 98 | + if not page_name: # None or '' |
| 99 | + return html.Div([ |
| 100 | + dcc.Link(href=app.get_relative_path('/page-1')), |
| 101 | + dcc.Link(href=app.get_relative_path('/page-2')), |
| 102 | + ]) |
| 103 | + elif page_name == 'page-1': |
| 104 | + return chapters.page_1 |
| 105 | + if page_name == "page-2": |
| 106 | + return chapters.page_2 |
| 107 | + ``` |
| 108 | + Note that `chapters.page_1` will be served if the user visits `/page-1` |
| 109 | + _or_ `/page-1/` since `strip_relative_path` removes the trailing slash. |
| 110 | +
|
| 111 | + Also note that `strip_relative_path` is compatible with |
| 112 | + `get_relative_path` in environments where `requests_pathname_prefix` set. |
| 113 | + In some deployment environments, like Dash Enterprise, |
| 114 | + `requests_pathname_prefix` is set to the application name, e.g. `my-dash-app`. |
| 115 | + When working locally, `requests_pathname_prefix` might be unset and |
| 116 | + so a relative URL like `/page-2` can just be `/page-2`. |
| 117 | + However, when the app is deployed to a URL like `/my-dash-app`, then |
| 118 | + `app.get_relative_path('/page-2')` will return `/my-dash-app/page-2` |
| 119 | +
|
| 120 | + The `pathname` property of `dcc.Location` will return '`/my-dash-app/page-2`' |
| 121 | + to the callback. |
| 122 | + In this case, `app.strip_relative_path('/my-dash-app/page-2')` |
| 123 | + will return `'page-2'` |
| 124 | +
|
| 125 | + For nested URLs, slashes are still included: |
| 126 | + `app.strip_relative_path('/page-1/sub-page-1/')` will return |
| 127 | + `page-1/sub-page-1` |
| 128 | + ``` |
| 129 | + """ |
| 130 | + return app_strip_relative_path(CONFIG.requests_pathname_prefix, path) |
| 131 | + |
| 132 | + |
| 133 | +def app_strip_relative_path(requests_pathname, path): |
| 134 | + if path is None: |
| 135 | + return None |
| 136 | + if ( |
| 137 | + requests_pathname != "/" and not path.startswith(requests_pathname.rstrip("/")) |
| 138 | + ) or (requests_pathname == "/" and not path.startswith("/")): |
| 139 | + raise exceptions.UnsupportedRelativePath( |
| 140 | + """ |
| 141 | + Paths that aren't prefixed with requests_pathname_prefix are not supported. |
| 142 | + You supplied: {} and requests_pathname_prefix was {} |
| 143 | + """.format( |
| 144 | + path, requests_pathname |
| 145 | + ) |
| 146 | + ) |
| 147 | + if requests_pathname != "/" and path.startswith(requests_pathname.rstrip("/")): |
| 148 | + path = path.replace( |
| 149 | + # handle the case where the path might be `/my-dash-app` |
| 150 | + # but the requests_pathname_prefix is `/my-dash-app/` |
| 151 | + requests_pathname.rstrip("/"), |
| 152 | + "", |
| 153 | + 1, |
| 154 | + ) |
| 155 | + return path.strip("/") |
0 commit comments