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

Unable to mount to path containing variable #793

Closed
ghost opened this issue Oct 9, 2015 · 5 comments
Closed

Unable to mount to path containing variable #793

ghost opened this issue Oct 9, 2015 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 9, 2015

No description provided.

@RonRothman
Copy link

Hi Brandon,

Is it not sufficient to put the parameter in the route (as opposed to in
the mount point)?

bottle.mount('/users', app)

@app.get('/<user_id>')
def index(user_id):
return user_id

On Fri, Oct 9, 2015 at 6:30 PM, Brandon Hsiao notifications@github.com
wrote:

I'd like to mount to a path containing a variable. For example, if I were
to mount app to /lol/ I'd like the callback sent to app.get to receive
a parameter x, in addition to all further variables. I tried this:

import bottle

app = bottle.Bottle()
bottle.mount('/lol/', app)

@app.get('/')
def index(x):
return x

bottle.run()

which unfortunately gave me this error:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
TypeError: mountpoint_wrapper() takes no arguments (1 given)

This is actually a fairly common requirement, e.g. in mounting
/users/<user_id> to a user app when building a REST API. Is this possible?


Reply to this email directly or view it on GitHub
#793.

@RonRothman
Copy link

No, I don't think it does. Wouldn't it still useful be to decompose an app
this way, say?

bottle.mount('/users', user_app)
bottle.mount('/
​projects​
',
​project
_app)

@
​user_​
app.get('/<user_id>')
def index(user_id):
return user_id

@project_app.get('/<project_id>')
def index(project_id):
return project_id
​...

On Sat, Oct 10, 2015 at 4:19 PM, Brandon Hsiao notifications@github.com
wrote:

Sure! But doesn't that sort of defeat the purpose of mounting subapps?


Reply to this email directly or view it on GitHub
#793 (comment).

@RonRothman
Copy link

Thanks for the explanation. After mulling it over, I suppose I find your
first two benefits unconvincing. (1) You can install plugins at the app
level without having to specify a decorator repeatedly. (2) You can create
the mount points programmatically if you won't want to repeat the literal
"<user_id:int>".

I do find your third point more apt--but since I've never designed an app
with that particular structure, I'm afraid I can't really speak to the use
case. Good luck!
-ron

On Sat, Oct 10, 2015 at 9:33 PM, Brandon Hsiao notifications@github.com
wrote:

It feels to me the top two benefits of subapps are (1) subapp-specific
plugins and (2) not having to type the mount point over and over. In the
case of mounting /users/<user_id:int>, I might e.g. write a plugin that
fetches the user based on the user ID, aborts if it's not found, and pass
the user instead of the user ID to the function. I'd want to mount that
specifically to /users/<user_id:int>, and not have to use a decorator
over and over. And having to write <user_id:int> repeatedly is not only
clunky but arguably a violation of DRY.

Your example doesn't really capture the pain point of this "bug" as each
of your mounted apps only has one endpoint, but once you have to manually
add users/<user_id:int>/messages, /users/<user_id:int>/hobbies,
/users/<user_id:int>/fetishes, and ten others, you start getting uneasy.


Reply to this email directly or view it on GitHub
#793 (comment).

@innov8ian
Copy link
Contributor

From my view the logic for mounting with a variable in the mount point does not seem to really fit.
To me it seems an app should be independent of the mount point, and not require recoding to work at different mount points. If there is a variable in the mount point, then every route handler is receiving a an additional parameter not contained in the route itself?
To me this does not read well, and does not extend to the case where an app is in a separate file.

However I do sympathise with you having to tyle '/<user_id:int>/mesage' '/<user_id:int>/hobies' etc each time.

I made a filter that may be of some help. It allow for multiple choice. It is designed for file name, but would work for your case.
you could have:

  @get('<user_id:int>/<function:fileExt:message,hobbies,fetishes')
  def userFuncs(user_id,function):
      ...

Not sure if it helps, but if it does I will post the code.

@adnanoner
Copy link

Hi @innov8ian , would be interested in some extra code with an example 👍

@ghost ghost closed this as completed May 25, 2018
This issue was closed.
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

No branches or pull requests

3 participants