views:

115

answers:

7

Is there a disadvantage to using a dynamic Python file to generate the CSS for a webpage? I'd like computers with an administrator cookie to show special admin panel CSS, and show regular CSS for all other users. I'm planning to use:

<link rel="stylesheet" href="/css.py" type="text/css" />
+9  A: 

That should work fine.

I hope, however, that you're not relying on CSS alone to restrict admin functionality.

SLaks
+7  A: 

You can do that. Just realize that there will be a performance hit on each page. CSS files are usually cached as they do not change often.

In theory you do not need to even use an <link> as you can render the style right in the page as a <style>, as it will be refreshed every page request.

Glennular
This can be cached, too.
SLaks
I think the initial idea was to reload the style on every request. If the parent page is not being cached then the dynamic CSS might not be cache-able as well.
Glennular
A: 

The downside to using a dynamic file is that it won't be as fast as a static file. In all likelihood, that's not really a problem (the difference is likely negligible). If it later turns out it is a problem, you could just precompute the different CSS and link in the appropriate one.

On an unrelated note, it's not really safe to trust a cookie to determine if the user is an administrator.

Hank Gay
Thanks for the tip. Can you point me to where I can learn more about having more secure logins? (ie: more than just cookies)
Yongho
Ah. The question wasn't clear that it was a session cookie for identifying a user; I misinterpreted it to mean that it was a cookie containing something like "admin=true". Session cookies are a much safer approach, although there is still a lot of work to make sure they are secure. See [the Wikipedia entry](http://en.wikipedia.org/wiki/HTTP_cookie) for a complete list. Realistically, handling those sorts of things are what a good web framework is for.
Hank Gay
+4  A: 
  • You will need to specify the correct content type when generating the resource
  • You will probably need to be explicit about issuing cache control headers

It probably isn't worth the effort. If there is a lot of admin panel specific CSS, then just have a second <link> element if logged in as an admin, otherwise just merge it with the main file.

David Dorward
+1 - you can spend a lot of time getting the caching right, but it's almost never worth it. A separate admin stylesheet included on the appropriate pages is likely to be easier and faster.
bobince
I would agree with this approach. Aside from the client side caching there are also benefits on server side to be gained by keeping files static (server side caching, no script processing...)
aepheus
A: 

If www.example.com/css.py is accessible by a web browser and produces valid css it should work, at least with php it does. maybe you just need to pass a content-type: text/css HTTP header

However I think you could set special classes in the HTML page generation in case of this cookie presence and let a static css handle them, but if you really need to do this I see no other disadvantage apart than a little more processing time.

maid450
+1  A: 

Extensions don't matter. You can end it in .jpg if you want. What does (somehow) matter are the headers. You should send the following header:

Content-Type: text/css; charset=UTF-8

Still, if you don't add that, you won't run into any (immediate) problems. As long as you use type="text/css" in HTML the browser will know what you're talking about.

Note that, as Glennular said, CSS files are cached. Thus, you might want to use something like:

<link rel="stylesheet" href="/css.py?SOME_RANDOM_SEED" type="text/css" />

Where SOME_RANDOM_SEED is, obviously, a random character sequence. You can use time.time() or uuid.uuid4() or something similar.

Felix
A: 

Although using dynamically created CSS works nowadays, I fear that you can't rely on it being changed depending on a cookie.

CSS files are cached more heavily than the pages themselves, so even if you try to prevent the file from being cached, some browsers might hold on to an old version longer than they should.

So, if you are thinking of using the CSS for showing or hiding features depending on a cookie, that's not a stable solution.

Guffa