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, y
(self, path: str)
| 1869 | ) |
| 1870 | |
| 1871 | def strip_relative_path(self, path: str) -> Union[str, None]: |
| 1872 | """ |
| 1873 | Return a path with `requests_pathname_prefix` and leading and trailing |
| 1874 | slashes stripped from it. Also, if None is passed in, None is returned. |
| 1875 | Use this function with `get_relative_path` in callbacks that deal |
| 1876 | with `dcc.Location` `pathname` routing. |
| 1877 | That is, your usage may look like: |
| 1878 | ``` |
| 1879 | app.layout = html.Div([ |
| 1880 | dcc.Location(id='url'), |
| 1881 | html.Div(id='content') |
| 1882 | ]) |
| 1883 | @app.callback(Output('content', 'children'), [Input('url', 'pathname')]) |
| 1884 | def display_content(path): |
| 1885 | page_name = app.strip_relative_path(path) |
| 1886 | if not page_name: # None or '' |
| 1887 | return html.Div([ |
| 1888 | dcc.Link(href=app.get_relative_path('/page-1')), |
| 1889 | dcc.Link(href=app.get_relative_path('/page-2')), |
| 1890 | ]) |
| 1891 | elif page_name == 'page-1': |
| 1892 | return chapters.page_1 |
| 1893 | if page_name == "page-2": |
| 1894 | return chapters.page_2 |
| 1895 | ``` |
| 1896 | Note that `chapters.page_1` will be served if the user visits `/page-1` |
| 1897 | _or_ `/page-1/` since `strip_relative_path` removes the trailing slash. |
| 1898 | |
| 1899 | Also note that `strip_relative_path` is compatible with |
| 1900 | `get_relative_path` in environments where `requests_pathname_prefix` set. |
| 1901 | In some deployment environments, like Dash Enterprise, |
| 1902 | `requests_pathname_prefix` is set to the application name, e.g. `my-dash-app`. |
| 1903 | When working locally, `requests_pathname_prefix` might be unset and |
| 1904 | so a relative URL like `/page-2` can just be `/page-2`. |
| 1905 | However, when the app is deployed to a URL like `/my-dash-app`, then |
| 1906 | `app.get_relative_path('/page-2')` will return `/my-dash-app/page-2` |
| 1907 | |
| 1908 | The `pathname` property of `dcc.Location` will return '`/my-dash-app/page-2`' |
| 1909 | to the callback. |
| 1910 | In this case, `app.strip_relative_path('/my-dash-app/page-2')` |
| 1911 | will return `'page-2'` |
| 1912 | |
| 1913 | For nested URLs, slashes are still included: |
| 1914 | `app.strip_relative_path('/page-1/sub-page-1/')` will return |
| 1915 | `page-1/sub-page-1` |
| 1916 | ``` |
| 1917 | """ |
| 1918 | return _get_paths.app_strip_relative_path( |
| 1919 | self.config.requests_pathname_prefix, path |
| 1920 | ) |
| 1921 | |
| 1922 | @staticmethod |
| 1923 | def add_startup_route( |
no outgoing calls