Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use underscore in route method names #456

Closed
simonmesmith opened this issue Feb 6, 2023 · 9 comments
Closed

Can't use underscore in route method names #456

simonmesmith opened this issue Feb 6, 2023 · 9 comments
Labels
documentation Improvements or additions to documentation

Comments

@simonmesmith
Copy link

simonmesmith commented Feb 6, 2023

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.

@Lendemor
Copy link
Collaborator

Lendemor commented Feb 6, 2023

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.

@simonmesmith
Copy link
Author

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

@Lendemor
Copy link
Collaborator

Lendemor commented Feb 6, 2023

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")

@simonmesmith
Copy link
Author

Thanks. Glad it wasn't just me!

@picklelo
Copy link
Contributor

picklelo commented Feb 6, 2023

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.

@simonmesmith
Copy link
Author

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.

@jqwez
Copy link
Contributor

jqwez commented Feb 12, 2023

@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.

@Alek99 Alek99 added the bug Something isn't working label Feb 15, 2023
@jp-0
Copy link

jp-0 commented May 6, 2023

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

@ElijahAhianyo ElijahAhianyo added documentation Improvements or additions to documentation and removed bug Something isn't working labels Aug 18, 2023
@picklelo
Copy link
Contributor

picklelo commented Sep 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

7 participants