librelist archives

« back to archive

Static Folder Configuration - Returning HTTP 400 (Debug View)

Static Folder Configuration - Returning HTTP 400 (Debug View)

From:
Kurtis Mullins
Date:
2013-04-30 @ 16:50
Hello,

I'm trying to setup a custom static folder for development purposes. First,
I'll introduce my motivation and general environment. Then, I'll include
some details on what I've tried and what appears to be failing.

Our project is split in to two pieces. First, we have a repository which
includes our front-end HTML5 client. This is built on a JavaScript
framework and, aside from an API Flask is providing, is completely separate
from the Web Application. Second, we have a Web Application that we are
developing on top of Flask.

During development, I would like Flask to serve both the 'index.html'
(included as a 'template' without any specific template features) and our
static files (JavaScript, CSS, Images, etc...). This is desired to
eliminate the need for the developers (primarily, myself) to have to setup
Nginx or another front-end server on every machine they use. Also, there
are issues with cross-domain requests if I simply load the index.html as a
file (in the browser) and then try to send my requests off to
http://localhost:5000.

So, I read the documentation and tried to do some Google-ing as well. I
haven't had a whole lot of luck but I did come across two configuration
variables that should setup the 'static' directory appropriately. These
are: 'static_folder' and 'static_url_path'.

The entire code-base for our front-end is in one directory. For the sake of
making things easy, I will use a shortened name. We are using absolute
paths. Here is my general configuration.

web_dir = '/home/kurtis/Projects/front-end'
app.config['template_folder'] = web_dir
app.config['static_folder'] = web_dir
app.config['static_url_path'] = 'static'

Please note that I have tried various combinations of leading and trailing
slashes on the static_url_path. As far as I can tell, the web_dir is fine
because my over-ride on the Jinja configuration is serving index.html
correctly.

So, ideally, our site should load according to this:

http://localhost:5000 - Serves index.html
http://localhost/static/css/project.css - Should serve the expected file.

As far as the directory structure goes, the web_dir is basically:

index.html
css/
libs/
src/
etc...

In conclusion, my index.html file (loaded as a template) works fine but any
other files that are attempted to be accessed return a 400. Any suggestions
on what I'm doing wrong?

Thank you

Re: [flask] Static Folder Configuration - Returning HTTP 400 (Debug View)

From:
Sandeep Raju
Date:
2013-04-30 @ 16:55
On Tue, Apr 30, 2013 at 10:20 PM, Kurtis Mullins
<kurtis.mullins@gmail.com>wrote:

> Hello,
>
> I'm trying to setup a custom static folder for development purposes.
> First, I'll introduce my motivation and general environment. Then, I'll
> include some details on what I've tried and what appears to be failing.
>
> Our project is split in to two pieces. First, we have a repository which
> includes our front-end HTML5 client. This is built on a JavaScript
> framework and, aside from an API Flask is providing, is completely separate
> from the Web Application. Second, we have a Web Application that we are
> developing on top of Flask.
>
> During development, I would like Flask to serve both the 'index.html'
> (included as a 'template' without any specific template features) and our
> static files (JavaScript, CSS, Images, etc...). This is desired to
> eliminate the need for the developers (primarily, myself) to have to setup
> Nginx or another front-end server on every machine they use. Also, there
> are issues with cross-domain requests if I simply load the index.html as a
> file (in the browser) and then try to send my requests off to
> http://localhost:5000.
>
> So, I read the documentation and tried to do some Google-ing as well. I
> haven't had a whole lot of luck but I did come across two configuration
> variables that should setup the 'static' directory appropriately. These
> are: 'static_folder' and 'static_url_path'.
>
> The entire code-base for our front-end is in one directory. For the sake
> of making things easy, I will use a shortened name. We are using absolute
> paths. Here is my general configuration.
>
> web_dir = '/home/kurtis/Projects/front-end'
> app.config['template_folder'] = web_dir
> app.config['static_folder'] = web_dir
> app.config['static_url_path'] = 'static'
>
> Please note that I have tried various combinations of leading and trailing
> slashes on the static_url_path. As far as I can tell, the web_dir is fine
> because my over-ride on the Jinja configuration is serving index.html
> correctly.
>
> So, ideally, our site should load according to this:
>
> http://localhost:5000 - Serves index.html
> http://localhost/static/css/project.css - Should serve the expected file.
>

Shouldn't the static file url be,
http://localhost:5000/static/css/project.css ?

>
> As far as the directory structure goes, the web_dir is basically:
>
> index.html
> css/
> libs/
> src/
> etc...
>
> In conclusion, my index.html file (loaded as a template) works fine but
> any other files that are attempted to be accessed return a 400. Any
> suggestions on what I'm doing wrong?
>
> Thank you
>



-- 
Sandeep Raju
CSE
PESIT
Bangalore

Re: [flask] Static Folder Configuration - Returning HTTP 400 (Debug View)

From:
Kurtis Mullins
Date:
2013-04-30 @ 17:25
Yep, sorry that was a typo on my point.

On another note, I got it figured out with the help of those in IRC. My
problem is that I wasn't sending my template_folder, static_folder, and
static_url_path as arguments to my App(...) initializer.

Here's the solution in case anyone else gets stuck:

app = Flask(__name__, static_folder=web_dir, template_folder=web_dir,
static_url_path='/static')


On Tue, Apr 30, 2013 at 12:55 PM, Sandeep Raju <sandeep080@gmail.com> wrote:

>
>
>
> On Tue, Apr 30, 2013 at 10:20 PM, Kurtis Mullins <kurtis.mullins@gmail.com
> > wrote:
>
>> Hello,
>>
>> I'm trying to setup a custom static folder for development purposes.
>> First, I'll introduce my motivation and general environment. Then, I'll
>> include some details on what I've tried and what appears to be failing.
>>
>> Our project is split in to two pieces. First, we have a repository which
>> includes our front-end HTML5 client. This is built on a JavaScript
>> framework and, aside from an API Flask is providing, is completely separate
>> from the Web Application. Second, we have a Web Application that we are
>> developing on top of Flask.
>>
>> During development, I would like Flask to serve both the 'index.html'
>> (included as a 'template' without any specific template features) and our
>> static files (JavaScript, CSS, Images, etc...). This is desired to
>> eliminate the need for the developers (primarily, myself) to have to setup
>> Nginx or another front-end server on every machine they use. Also, there
>> are issues with cross-domain requests if I simply load the index.html as a
>> file (in the browser) and then try to send my requests off to
>> http://localhost:5000.
>>
>> So, I read the documentation and tried to do some Google-ing as well. I
>> haven't had a whole lot of luck but I did come across two configuration
>> variables that should setup the 'static' directory appropriately. These
>> are: 'static_folder' and 'static_url_path'.
>>
>> The entire code-base for our front-end is in one directory. For the sake
>> of making things easy, I will use a shortened name. We are using absolute
>> paths. Here is my general configuration.
>>
>> web_dir = '/home/kurtis/Projects/front-end'
>> app.config['template_folder'] = web_dir
>> app.config['static_folder'] = web_dir
>> app.config['static_url_path'] = 'static'
>>
>> Please note that I have tried various combinations of leading and
>> trailing slashes on the static_url_path. As far as I can tell, the web_dir
>> is fine because my over-ride on the Jinja configuration is serving
>> index.html correctly.
>>
>> So, ideally, our site should load according to this:
>>
>> http://localhost:5000 - Serves index.html
>> http://localhost/static/css/project.css - Should serve the expected file.
>>
>
> Shouldn't the static file url be,
> http://localhost:5000/static/css/project.css ?
>
>>
>> As far as the directory structure goes, the web_dir is basically:
>>
>> index.html
>> css/
>> libs/
>> src/
>> etc...
>>
>> In conclusion, my index.html file (loaded as a template) works fine but
>> any other files that are attempted to be accessed return a 400. Any
>> suggestions on what I'm doing wrong?
>>
>> Thank you
>>
>
>
>
> --
> Sandeep Raju
> CSE
> PESIT
> Bangalore
>