reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Can't use underscore in route method names

Open simonmesmith opened this issue 3 years ago β€’ 7 comments

Describe the bug I get an error when using a route method name like def sign_up() versus def signup(). While a minor annoyance, this conflicts with common Python code style.

To Reproduce Steps to reproduce the behavior:

  • Create a route function like def sign_up() and add it to your routes
  • Attempt to navigate to http://localhost:3000/sign_up
  • You will get an unhandled error
  • Note that this also seems to happen if you try to use signUp(); only signup() works

Expected behavior You should be able to use underscores in route method names.

Screenshots Not necessary.

Specifics (please complete the following information):

  • Python Version: 3.10.8
  • Pynecone Version: 0.1.14
  • OS: macOS Monterey (12.6.2)

Additional context I don't think any additional context is necessary. But if I'm doing something wrong, let me know.

simonmesmith avatar Feb 06 '23 08:02 simonmesmith

It's because pynecone try to guess the actual route based on your method name.

Can you try to add the keyword arg route="sign_up" in the add_page() method? It should allow you to use any valid method name this way.

Lendemor avatar Feb 06 '23 10:02 Lendemor

Tried. Still doesn't seem to work, unfortunately.

simonmesmith avatar Feb 06 '23 11:02 simonmesmith

Okay I reproduced the problem, there is indeed a bug if you put an underscore in the route name.

Meanwhile, the following should work until the bug is resolved:

app.add_page(sign_up, route="signup")

or

app.add_page(signUp, route="signup")

Lendemor avatar Feb 06 '23 11:02 Lendemor

Thanks. Glad it wasn't just me!

simonmesmith avatar Feb 06 '23 11:02 simonmesmith

We automatically convert underscores to hyphens - so the route should be available at sign-up - lmk if that works.

We should either document this better or we can change this logic if people like.

picklelo avatar Feb 06 '23 22:02 picklelo

I can confirm that the_route gets converted to the-route and it works!

I don't feel strongly that this has to be rendered with the underscore.

This said, it might be easier to render it with the underscore instead of converting it to a hyphen all else being equal, because it will prevent people from getting confused and having to find an answer in the documentation.

simonmesmith avatar Feb 07 '23 12:02 simonmesmith

@picklelo Is there a reason why routes convert underscore to hyphens? I see the line of code in app.py line: 1032

route = to_snake_case(route).replace("_", "-")

Is there history behind this or is it just based on hyphens being more popular for routes than underscores? I personally go either way on this but to close this issue out we should either nix that line and let people run with underscores or we could put a warning on https://pynecone.io/docs/components/pages under "adding a page" that tells people of this behavior.

jqwez avatar Feb 12 '23 13:02 jqwez

Hi all, this behaviour was causing issues for me also and took a little while to understand what was happening, so thank you for this.

I think it is more appropriate to change the logic. This conversion is applied to all routes, meaning the behaviour is particularly confusing where an explicit route is provided as there is no way to define a route with an underscore.

e.g. app.add_page(page, route='page_1') has format_route applied and routed as /page-1 instead of the explicitly provided /page_1.

https://github.com/pynecone-io/pynecone/blob/a24d382eb61959e7cc309ea00ca906ca8c30cda3/pynecone/app.py#L286

jp-0 avatar May 06 '23 09:05 jp-0

This is resolved in #1713 - if you use add_page and provide a route name, we will keep the underscores. However, if the route is automatically inferred from the function name, the underscore is replaced with a hyphen.

picklelo avatar Sep 12 '23 18:09 picklelo