librelist archives

« back to archive

Specify Flask.root_path

Specify Flask.root_path

From:
Mayowa Akinyemi
Date:
2010-07-18 @ 18:59
Hi,

Is there a way to specify the root path in flask?
The need for this arose after packing my app with py2exe and the application
could no longer find templates or static files.

A little digging into the code quickly revealed that:
1. class Flask inherits a root_path attribute from it base class
_PackageBoundObject
2. class _PackageBoundObject infers the location from the module name (which
is altered when the app is frozen hence the problem)

I'm not sure that sub-classing Flask will work in this case as any value set
in self.root_path will be overwritten in _PackageBoundObject.__init__()
I've worked around this by patching Flask and _PackageBoundObject and it
works.

I'm wondering if there is another way that doesnt require altering flask.

Thank you.

Re: [flask] Specify Flask.root_path

From:
Armin Ronacher
Date:
2010-07-19 @ 00:55
Hi,

On 7/18/10 8:59 PM, Mayowa Akinyemi wrote:
> Is there a way to specify the root path in flask?
> The need for this arose after packing my app with py2exe and the
> application could no longer find templates or static files.
Create a subclass of Flask that sets the root_path after calling the 
superclass'es __init__.  It might be possible to detect py2exe of other 
frozen environments, but I fail to see the usecase of py2exe right now.


Regards,
Armin

Re: [flask] Specify Flask.root_path

From:
Mayowa Akinyemi
Date:
2010-07-19 @ 01:05
> but I fail to see the usecase of py2exe right now.
Ah the luxuries enjoyed by my brethren who use the *nixes...
Sadly those of us stuck in windows have no choice in some matters...

I'm not so sure subclassing would work:

helpers.py
class _PackageBoundObject(object):

    def __init__(self, import_name):
        #: The name of the package or module.  Do not change this once
        #: it was set by the constructor.
        self.import_name = import_name

        #: Where is the app root located?
        self.root_path = _get_package_path(self.import_name) # <--- this
here is the problem; it would overide anything i set in the subclass

However I'll give it a try and get back to you.

Thanks.

On Mon, Jul 19, 2010 at 1:55 AM, Armin Ronacher <armin.ronacher@active-4.com
> wrote:

> Hi,
>
> On 7/18/10 8:59 PM, Mayowa Akinyemi wrote:
> > Is there a way to specify the root path in flask?
> > The need for this arose after packing my app with py2exe and the
> > application could no longer find templates or static files.
> Create a subclass of Flask that sets the root_path after calling the
> superclass'es __init__.  It might be possible to detect py2exe of other
> frozen environments, but I fail to see the usecase of py2exe right now.
>
>
> Regards,
> Armin
>

Re: [flask] Specify Flask.root_path

From:
Mayowa Akinyemi
Date:
2010-07-19 @ 01:20
I've tried it with mixed results.
When the script is run from python it works, when the script is frozen
however, it can find templates but not the static folder.

Any thoughts about what could be responsible for this behavior?

On Mon, Jul 19, 2010 at 2:05 AM, Mayowa Akinyemi <mayowa@gmail.com> wrote:

> > but I fail to see the usecase of py2exe right now.
> Ah the luxuries enjoyed by my brethren who use the *nixes...
> Sadly those of us stuck in windows have no choice in some matters...
>
> I'm not so sure subclassing would work:
>
> helpers.py
> class _PackageBoundObject(object):
>
>     def __init__(self, import_name):
>         #: The name of the package or module.  Do not change this once
>         #: it was set by the constructor.
>         self.import_name = import_name
>
>         #: Where is the app root located?
>         self.root_path = _get_package_path(self.import_name) # <--- this
> here is the problem; it would overide anything i set in the subclass
>
> However I'll give it a try and get back to you.
>
> Thanks.
>
>
> On Mon, Jul 19, 2010 at 1:55 AM, Armin Ronacher <
> armin.ronacher@active-4.com> wrote:
>
>> Hi,
>>
>> On 7/18/10 8:59 PM, Mayowa Akinyemi wrote:
>> > Is there a way to specify the root path in flask?
>> > The need for this arose after packing my app with py2exe and the
>> > application could no longer find templates or static files.
>> Create a subclass of Flask that sets the root_path after calling the
>> superclass'es __init__.  It might be possible to detect py2exe of other
>> frozen environments, but I fail to see the usecase of py2exe right now.
>>
>>
>> Regards,
>> Armin
>>
>
>

Re: [flask] Specify Flask.root_path

From:
Mayowa Akinyemi
Date:
2010-07-19 @ 01:32
Ok I figured it out:

class RFlask(Flask):
    def __init__(self, import_name, static_path=None, root_path=None):

        Flask.__init__(self, import_name, static_path)
        self.root_path = root_path

        # if there is a static folder, register it for the application.
        if self.has_static_folder:
            self.add_url_rule(self.static_path + '/<path:filename>',
                              endpoint='static',
                              view_func=self.send_static_file)

Apparently setting self.root_path isn't sufficient...

Thanks.

On Mon, Jul 19, 2010 at 2:20 AM, Mayowa Akinyemi <mayowa@gmail.com> wrote:

> I've tried it with mixed results.
> When the script is run from python it works, when the script is frozen
> however, it can find templates but not the static folder.
>
> Any thoughts about what could be responsible for this behavior?
>
>
> On Mon, Jul 19, 2010 at 2:05 AM, Mayowa Akinyemi <mayowa@gmail.com> wrote:
>
>> > but I fail to see the usecase of py2exe right now.
>> Ah the luxuries enjoyed by my brethren who use the *nixes...
>> Sadly those of us stuck in windows have no choice in some matters...
>>
>> I'm not so sure subclassing would work:
>>
>> helpers.py
>> class _PackageBoundObject(object):
>>
>>     def __init__(self, import_name):
>>         #: The name of the package or module.  Do not change this once
>>         #: it was set by the constructor.
>>         self.import_name = import_name
>>
>>         #: Where is the app root located?
>>         self.root_path = _get_package_path(self.import_name) # <--- this
>> here is the problem; it would overide anything i set in the subclass
>>
>> However I'll give it a try and get back to you.
>>
>> Thanks.
>>
>>
>> On Mon, Jul 19, 2010 at 1:55 AM, Armin Ronacher <
>> armin.ronacher@active-4.com> wrote:
>>
>>> Hi,
>>>
>>> On 7/18/10 8:59 PM, Mayowa Akinyemi wrote:
>>> > Is there a way to specify the root path in flask?
>>> > The need for this arose after packing my app with py2exe and the
>>> > application could no longer find templates or static files.
>>> Create a subclass of Flask that sets the root_path after calling the
>>> superclass'es __init__.  It might be possible to detect py2exe of other
>>> frozen environments, but I fail to see the usecase of py2exe right now.
>>>
>>>
>>> Regards,
>>> Armin
>>>
>>
>>
>

Re: [flask] Specify Flask.root_path

From:
Armin Ronacher
Date:
2010-07-19 @ 07:09
Hi,

On 7/19/10 3:32 AM, Mayowa Akinyemi wrote:
> Apparently setting self.root_path isn't sufficient...
It should be in 0.6.  Please try with the development version.  The 
has_static_folder check went away because it was also causing issues 
with GAE.


Regards,
Armin