Skip to content

Conversation

@kinghuang
Copy link
Contributor

@kinghuang kinghuang commented Oct 24, 2019

Issue: #525

Description: The report urls don't show a trailing slash. This works when Streamlit is running at the base URL because browsers will implicitly add a / for a request on the root. But, this does not work when a custom server.baseUrlPath is defined, resulting in a 404 Not Found.

This PR adds a redirect from no trailing slash to with trailing slash.

Before:

root@672e90be303e:/project# streamlit run --server.baseUrlPath abc sa/gui.py

  You can now view your Streamlit app in your browser.

  Network URL: http://172.17.0.2:8501/abc
  External URL: http://209.89.162.170:8501/abc
‣ ~ http -h http://127.0.0.1:8501/abc 
HTTP/1.1 404 Not Found
‣ ~ http -h http://127.0.0.1:8501/abc/
HTTP/1.1 200 OK

After:

root@672e90be303e:/project# streamlit run --server.baseUrlPath abc sa/gui.py

  You can now view your Streamlit app in your browser.

  Network URL: http://172.17.0.2:8501/abc
  External URL: http://209.89.162.170:8501/abc
‣ ~ http -h http://127.0.0.1:8501/abc 
HTTP/1.1 301 Moved Permanently
Location: /abc/
‣ ~ http -h http://127.0.0.1:8501/abc/
HTTP/1.1 200 OK

Contribution License Agreement

By submiting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@kinghuang kinghuang requested a review from a team as a code owner October 24, 2019 18:28
@kinghuang kinghuang closed this Oct 25, 2019
@kinghuang kinghuang deleted the 521-trailing-slash branch October 25, 2019 01:06
@kinghuang kinghuang restored the 521-trailing-slash branch October 25, 2019 01:07
@kinghuang
Copy link
Contributor Author

Oops, didn't mean to delete the branch!

@kinghuang kinghuang reopened this Oct 25, 2019
Add a kw-arg to make_url_path_regex that controls whether an optional
trailing slash is included at the end of the path pattern. This will be
used to generate a path without the trailing slash for redirects.
Create a simple request handler that simply uses the
tornado.web.addslash decorator to add a slash to the end of the request
path for GET requests.
Copy link
Contributor

@tvst tvst left a comment

Choose a reason for hiding this comment

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

This actually breaks the base case where the user has no baseUrlPath. To repro you need to bypass the dev server:

  1. make all (or make all-devel plus make build. Same thing)
  2. make install
  3. streamlit hello
    → a new tab will appear in your browser, but it won't load.

Here's a fix:

diff --git a/lib/streamlit/Report.py b/lib/streamlit/Report.py
index fb44b77a..7f0076da 100644
--- a/lib/streamlit/Report.py
+++ b/lib/streamlit/Report.py
@@ -56,7 +56,7 @@ class Report(object):
         if base_path:
             base_path = "/" + base_path

-        return "http://%(host_ip)s:%(port)s%(base_path)s/" % {
+        return "http://%(host_ip)s:%(port)s%(base_path)s" % {
             "host_ip": host_ip.strip("/"),
             "port": port,
             "base_path": base_path,
diff --git a/lib/streamlit/server/Server.py b/lib/streamlit/server/Server.py
index 5954bc0c..73565777 100644
--- a/lib/streamlit/server/Server.py
+++ b/lib/streamlit/server/Server.py
@@ -260,15 +260,15 @@ class Server(object):

             routes.extend(
                 [
-                    (
-                        make_url_path_regex(base, trailing_slash=False),
-                        AddSlashHandler
-                    ),
                     (
                         make_url_path_regex(base, "(.*)"),
                         StaticFileHandler,
                         {"path": "%s/" % static_path, "default_filename": "index.html"},
                     )
+                    (
+                        make_url_path_regex(base, trailing_slash=False),
+                        AddSlashHandler
+                    ),
                 ]
             )

This also has the benefit of not adding a final / to the path printed on the console. Because http://localhost:1234 looks nicer than http://localhost:1234/ 🤓

@tvst
Copy link
Contributor

tvst commented Oct 25, 2019

Forgot to say: You're on a roll!!!! 💯

@kinghuang
Copy link
Contributor Author

Haha, alright. I'll remove the trailing slash in the report url!

If a request comes in for the app on the base path without a trailing
slash, respond with a redirect to the path with the trailing slash.
Otherwise, users just receive a 404 Not Found.
Add an extra test case ensuring that trailing slashes are removed.
Python 2.7 doesn’t support having keyword args after an args collector.
Replacing trailing_slash with gathering all keyword args, and then
extract it from there.
@tvst tvst merged commit 6e57fb4 into streamlit:develop Oct 25, 2019
@kinghuang
Copy link
Contributor Author

I've updated the description in the PR to remove the trailing slash on the report URL.

@kinghuang kinghuang deleted the 521-trailing-slash branch October 25, 2019 14:05
sthagen added a commit to sthagen/streamlit-streamlit that referenced this pull request Oct 26, 2019
tconkling added a commit to tconkling/streamlit that referenced this pull request Oct 29, 2019
* develop:
  Use custom context manager for temporary file in cli.py (streamlit#489)
  Update pull_request_template.md
  Update pull_request_template.md
  Handle lack of trailing slash (streamlit#528)
  Set auto_envvar_prefix to STREAMLIT (streamlit#477)
  Strip slashes in path components (streamlit#523)
  Create doc_improvement.md
  ESlint: allow @ts-ignore (streamlit#499)
  Update Pipfile (streamlit#505)
  Release 0.49.0 (streamlit#509)
  Removing package json (streamlit#501)
  Feature/input number (streamlit#416)
  ESLint warnings are fatal in Circle (streamlit#492)
  Doc updates (streamlit#286)
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.

2 participants