librelist archives

« back to archive

app.route with class method.

app.route with class method.

From:
Spacelee
Date:
2013-01-07 @ 06:17
I have such a program, but when I access http://localhost:5000/foo, it
tells :

127.0.0.1 - - [07/Jan/2013 14:12:15] "GET /foo HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in
full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in
full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in
dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: foo() takes exactly 1 argument (0 given)



so how should I do with this kind of problem, as with class, we could just
declare a object just once and then  each of the method could access the
object directly, and it's easy to inherit from a base model or controller
so which could make things easier.



from flask import Flask
app = Flask(__name__)


@app.route("/")
def hello():
    return "Hello World!"


class A:

    def __init__(self):
        self.a = 'foo'

    @app.route('/foo')
    def foo(self):
        return self.a

if __name__ == "__main__":
    app.run(debug=True)

-- 
*Space Lee*

Re: [flask] app.route with class method.

From:
Smartboy
Date:
2013-01-07 @ 06:47
Unfortunately, you cannot use the decorator method since the decorators run
when the module the class is in is imported, not at class instantiation.
You have to use add_url_rule in the __init__ method instead, as explained
here <http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule>.

Smartboy

On Sun, Jan 6, 2013 at 10:17 PM, Spacelee <fjctlzy@gmail.com> wrote:

> I have such a program, but when I access http://localhost:5000/foo, it
> tells :
>
> 127.0.0.1 - - [07/Jan/2013 14:12:15] "GET /foo HTTP/1.1" 500 -
> Traceback (most recent call last):
>   File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in __call__
>     return self.wsgi_app(environ, start_response)
>   File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in wsgi_app
>     response = self.make_response(self.handle_exception(e))
>   File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in wsgi_app
>     response = self.full_dispatch_request()
>   File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in
> full_dispatch_request
>     rv = self.handle_user_exception(e)
>   File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in
> full_dispatch_request
>     rv = self.dispatch_request()
>   File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in
> dispatch_request
>     return self.view_functions[rule.endpoint](**req.view_args)
> TypeError: foo() takes exactly 1 argument (0 given)
>
>
>
> so how should I do with this kind of problem, as with class, we could just
> declare a object just once and then  each of the method could access the
> object directly, and it's easy to inherit from a base model or controller
> so which could make things easier.
>
>
>
> from flask import Flask
> app = Flask(__name__)
>
>
> @app.route("/")
> def hello():
>     return "Hello World!"
>
>
> class A:
>
>     def __init__(self):
>         self.a = 'foo'
>
>     @app.route('/foo')
>     def foo(self):
>         return self.a
>
> if __name__ == "__main__":
>     app.run(debug=True)
>
> --
> *Space Lee*
>

Re: [flask] app.route with class method.

From:
Freedom Dumlao
Date:
2013-01-07 @ 15:40
Flask routes cannot be used 'as-is' on class methods. If you're interested
in constructing an app from classes like this consider Flask-Classy:
http://packages.python.org/Flask-Classy/

*disclaimer - I'm the creator of Flask-Classy


On Mon, Jan 7, 2013 at 1:47 AM, Smartboy <smartboyathome@gmail.com> wrote:

> Unfortunately, you cannot use the decorator method since the decorators
> run when the module the class is in is imported, not at class
> instantiation. You have to use add_url_rule in the __init__ method instead,
> as explained here <
> http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule>.
>
> Smartboy
>
>
> On Sun, Jan 6, 2013 at 10:17 PM, Spacelee <fjctlzy@gmail.com> wrote:
>
>> I have such a program, but when I access http://localhost:5000/foo, it
>> tells :
>>
>> 127.0.0.1 - - [07/Jan/2013 14:12:15] "GET /foo HTTP/1.1" 500 -
>> Traceback (most recent call last):
>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in
>> __call__
>>     return self.wsgi_app(environ, start_response)
>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in
>> wsgi_app
>>     response = self.make_response(self.handle_exception(e))
>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in
>> wsgi_app
>>     response = self.full_dispatch_request()
>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in
>> full_dispatch_request
>>     rv = self.handle_user_exception(e)
>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in
>> full_dispatch_request
>>     rv = self.dispatch_request()
>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in
>> dispatch_request
>>     return self.view_functions[rule.endpoint](**req.view_args)
>> TypeError: foo() takes exactly 1 argument (0 given)
>>
>>
>>
>> so how should I do with this kind of problem, as with class, we could
>> just declare a object just once and then  each of the method could access
>> the object directly, and it's easy to inherit from a base model or
>> controller so which could make things easier.
>>
>>
>>
>> from flask import Flask
>> app = Flask(__name__)
>>
>>
>> @app.route("/")
>> def hello():
>>     return "Hello World!"
>>
>>
>> class A:
>>
>>     def __init__(self):
>>         self.a = 'foo'
>>
>>     @app.route('/foo')
>>     def foo(self):
>>         return self.a
>>
>> if __name__ == "__main__":
>>     app.run(debug=True)
>>
>> --
>> *Space Lee*
>>
>
>

Re: [flask] app.route with class method.

From:
Mark Grey
Date:
2013-01-07 @ 15:44
Plugs for that extension.

We had it working very nicely in staging... made the REST interface make a
lot more sense.

On Mon, Jan 7, 2013 at 10:40 AM, Freedom Dumlao <freedomdumlao@gmail.com>wrote:

> Flask routes cannot be used 'as-is' on class methods. If you're interested
> in constructing an app from classes like this consider Flask-Classy:
> http://packages.python.org/Flask-Classy/
>
> *disclaimer - I'm the creator of Flask-Classy
>
>
> On Mon, Jan 7, 2013 at 1:47 AM, Smartboy <smartboyathome@gmail.com> wrote:
>
>> Unfortunately, you cannot use the decorator method since the decorators
>> run when the module the class is in is imported, not at class
>> instantiation. You have to use add_url_rule in the __init__ method instead,
>> as explained here <
>> http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule>.
>>
>> Smartboy
>>
>>
>> On Sun, Jan 6, 2013 at 10:17 PM, Spacelee <fjctlzy@gmail.com> wrote:
>>
>>> I have such a program, but when I access http://localhost:5000/foo, it
>>> tells :
>>>
>>> 127.0.0.1 - - [07/Jan/2013 14:12:15] "GET /foo HTTP/1.1" 500 -
>>> Traceback (most recent call last):
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in
>>> __call__
>>>     return self.wsgi_app(environ, start_response)
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in
>>> wsgi_app
>>>     response = self.make_response(self.handle_exception(e))
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in
>>> wsgi_app
>>>     response = self.full_dispatch_request()
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in
>>> full_dispatch_request
>>>     rv = self.handle_user_exception(e)
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in
>>> full_dispatch_request
>>>     rv = self.dispatch_request()
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in
>>> dispatch_request
>>>     return self.view_functions[rule.endpoint](**req.view_args)
>>> TypeError: foo() takes exactly 1 argument (0 given)
>>>
>>>
>>>
>>> so how should I do with this kind of problem, as with class, we could
>>> just declare a object just once and then  each of the method could access
>>> the object directly, and it's easy to inherit from a base model or
>>> controller so which could make things easier.
>>>
>>>
>>>
>>> from flask import Flask
>>> app = Flask(__name__)
>>>
>>>
>>> @app.route("/")
>>> def hello():
>>>     return "Hello World!"
>>>
>>>
>>> class A:
>>>
>>>     def __init__(self):
>>>         self.a = 'foo'
>>>
>>>     @app.route('/foo')
>>>     def foo(self):
>>>         return self.a
>>>
>>> if __name__ == "__main__":
>>>     app.run(debug=True)
>>>
>>> --
>>> *Space Lee*
>>>
>>
>>
>

Re: [flask] app.route with class method.

From:
Spacelee
Date:
2013-02-17 @ 00:07
Great ext, I have tried it, and work well till now, thanks for your work. ~


On Mon, Jan 7, 2013 at 11:40 PM, Freedom Dumlao <freedomdumlao@gmail.com>wrote:

> Flask routes cannot be used 'as-is' on class methods. If you're interested
> in constructing an app from classes like this consider Flask-Classy:
> http://packages.python.org/Flask-Classy/
>
> *disclaimer - I'm the creator of Flask-Classy
>
>
> On Mon, Jan 7, 2013 at 1:47 AM, Smartboy <smartboyathome@gmail.com> wrote:
>
>> Unfortunately, you cannot use the decorator method since the decorators
>> run when the module the class is in is imported, not at class
>> instantiation. You have to use add_url_rule in the __init__ method instead,
>> as explained here <
>> http://flask.pocoo.org/docs/api/#flask.Flask.add_url_rule>.
>>
>> Smartboy
>>
>>
>> On Sun, Jan 6, 2013 at 10:17 PM, Spacelee <fjctlzy@gmail.com> wrote:
>>
>>> I have such a program, but when I access http://localhost:5000/foo, it
>>> tells :
>>>
>>> 127.0.0.1 - - [07/Jan/2013 14:12:15] "GET /foo HTTP/1.1" 500 -
>>> Traceback (most recent call last):
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in
>>> __call__
>>>     return self.wsgi_app(environ, start_response)
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in
>>> wsgi_app
>>>     response = self.make_response(self.handle_exception(e))
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in
>>> wsgi_app
>>>     response = self.full_dispatch_request()
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in
>>> full_dispatch_request
>>>     rv = self.handle_user_exception(e)
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in
>>> full_dispatch_request
>>>     rv = self.dispatch_request()
>>>   File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in
>>> dispatch_request
>>>     return self.view_functions[rule.endpoint](**req.view_args)
>>> TypeError: foo() takes exactly 1 argument (0 given)
>>>
>>>
>>>
>>> so how should I do with this kind of problem, as with class, we could
>>> just declare a object just once and then  each of the method could access
>>> the object directly, and it's easy to inherit from a base model or
>>> controller so which could make things easier.
>>>
>>>
>>>
>>> from flask import Flask
>>> app = Flask(__name__)
>>>
>>>
>>> @app.route("/")
>>> def hello():
>>>     return "Hello World!"
>>>
>>>
>>> class A:
>>>
>>>     def __init__(self):
>>>         self.a = 'foo'
>>>
>>>     @app.route('/foo')
>>>     def foo(self):
>>>         return self.a
>>>
>>> if __name__ == "__main__":
>>>     app.run(debug=True)
>>>
>>> --
>>> *Space Lee*
>>>
>>
>>
>


-- 
*Space Lee*