Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre python (publicaciones antiguas, página 91)

MinCSS is amazing

I had this is­sue open in the bug tra­cker for Niko­la (my sta­tic si­te ge­ne­ra­to­r) for a long ti­me: "A­dd min­css su­ppor­t".

We­ll, no, it does­n't ha­ve it ye­t, but I did so­me re­sear­ch on whe­ther it would be wor­th addin­g. And bo­y, min­css im­press­ed the he­ck out of me.

You see, Niko­la's the­mes tend to use una­dul­te­red boots­tra­p, whi­ch means they ca­rry a lar­ge num­ber of things that are not us­ed in their CSS. Be­si­des, it uses se­ve­ral sty­les­hee­ts from do­cu­til­s, pyg­men­ts, and mo­re.

What min­css does is exa­mi­ne your HT­ML and your CSS, and re­mo­ve all the unu­s­ed CSS. So, I wro­te a script that exa­mi­nes the Niko­la ou­tput and ove­rw­ri­tes the CSS fi­les wi­th the mi­ni­mal things that are ac­tua­lly nee­ded the­re.

And the re­sul­t?

He­re is the be­fo­re/a­fter for ea­ch CSS fi­le in Niko­la's de­mo si­te:

bootstrap-responsive.min.css  16849  3251
bootstrap.min.css            106059 14737
code.css                       3670  2114
colorbox.css                   6457   774
rst.css                        6559  2581
theme.css                      1287  1061
                             140881 24518

But wai­t, Niko­la su­ppor­ts bund­ling all tho­se fi­les in­to a sin­gle lar­ge CSS fi­le to avoid ne­two­rk re­ques­ts (u­sing we­ba­sse­ts). Does it wo­rk in that ca­se too?

We­ll ye­s:

all-nocdn.css                167457 29496

But that is not all. The min­css fi­les are not mi­ni­fie­d. Pa­s­sing all-­no­cd­n.­css th­rou­gh Yui-­com­pres­sor sh­ri­nks it fur­ther to 20599 by­tes. Whi­ch, gzi­ppe­d, is a pal­try 4801 by­tes. That means the com­ple­te sty­ling of the who­le si­te is a sin­gle CSS fi­le le­ss than 5KB in si­ze.

Tha­t, is im­pres­si­ve.

Deploying Django Into My Cheap VPS

I am pre­pa­ring to open my cheap si­te-an­d-­blo­g-hos­ting ser­vi­ce to the pu­blic at so­me poin­t, so I nee­ded to do so­me groun­dwo­rk in­to de­plo­y­men­t. Con­si­der that the host that wi­ll run it wi­ll ha­ve ve­ry li­mited re­sour­ce­s, so I nee­ded to find lean and cheap so­lu­tions when po­s­si­ble, but at the sa­me ti­me, I want to achie­ve rea­so­na­ble re­lia­bi­li­ty and ea­se of de­plo­y­men­t.

Sin­ce this is a tes­ting ser­ve­r, I want it to ha­ve git mas­ter de­plo­ye­d. I do­n't want au­to­ma­tic de­plo­y­men­t, but I want to de­ploy often, mea­ning se­ve­ral ti­mes dai­l­y.

I pre­fe­rred sim­ple tools ins­tead of com­plex tool­s, li­gh­twe­ight tools wi­th just enou­gh fea­tu­res ins­tead of hea­vie­r, mo­re fu­ll­y-­fea­tu­red tool­s. Your choi­ces on ea­ch step could and pro­ba­bly should be di­ffe­rent than mi­ne, de­pen­ding on your si­tua­tio­n, re­qui­re­men­ts and per­so­nal pre­fe­ren­ce­s.

So, he­re's my no­tes from how it's do­ne cu­rren­tl­y. This is not meant as a HO­W­TO, just a des­crip­tion of what see­ms to be wo­rking we­ll enou­gh so fa­r.

Leer más…

(Re)Introducing Alva, a Nikola Server

Over a year ago (ti­me flie­s!) I pos­ted so­me­thing about a pro­ject ca­lled Al­va. Let me quo­te mysel­f:

Al­va is al­most the oppo­si­te of Niko­la. If Niko­la is about making sta­tic si­tes, Al­va is a dy­na­mic si­te. Ho­we­ve­r, as He­gel su­gges­ts, from the the­sis and the an­ti­the­sis co­mes the syn­the­sis.

So, Al­va is about dy­na­mi­ca­lly crea­ting sta­tic si­tes. If you want to ha­ve Niko­la in your ser­ver ins­tead of in your own com­pu­te­r, and ha­ve the con­ve­nien­ce of an on­li­ne tool, tha­t's the ni­che Al­va tries to fi­ll.

So, you would ins­ta­ll Al­va, and use it like any other we­b-­ba­sed blo­gging tool. Ye­t, be­hind the sce­nes, you would ha­ve Niko­la, and all the per­for­man­ce and se­cu­ri­ty be­ne­fi­ts of sta­tic si­tes.

And ma­y­be so­me­da­y, I (or so­meo­ne) wi­ll put up a mul­ti-u­ser ver­sion of Al­va, and you wi­ll be able to get hos­ted blogs, kno­wing all the da­ta is yours and you can lea­ve an­y­ti­me and do your own thin­g.

The appro­ach I was taking at the ti­me pro­ved to be un­suc­ce­ss­fu­l, and the­re we­re a few other fai­lu­res along the wa­y. Of cour­se, the pro­blem was in how I was appro­aching the ta­sk. So I did the ri­ght thin­g, and lear­ned how to do it "ri­gh­t".

Sti­ll not usa­ble, sti­ll not hos­ted an­ywhe­re, but al­ready se­mi-­func­tio­na­l: Al­va li­ves now

The­re's a lot of wo­rk sti­ll to be do­ne. But I now know how to do it. To pre­vent the usual ar­gu­men­ts, he­re is a li­ttle ex­pla­na­tion of mo­ti­va­tio­n, toolin­g, etc.


I want a way to host blogs ve­ry chea­pl­y. How chea­pl­y? I want at least 1000 rea­so­na­bly ac­ti­ve users in a $5 VP­S. That would make Al­va a rea­so­na­ble al­ter­na­ti­ve to hos­ted mul­ti-u­ser wor­dpress, whi­ch means it would be a rea­so­na­ble so­lu­tion (if se­tup is ea­sy enou­gh) for sma­ll-­to­-­me­dium or­ga­ni­za­tions whi­ch do­n't want to se­tup ex­pen­si­ve in­fras­truc­tu­re yet want to own their da­ta (thi­nk school­s, sma­ll bu­si­nesses, FLO­SS pro­jec­ts, etc.) I al­so want to pro­vi­de that ser­vi­ce, for free. Whi­ch is why ano­ther rea­son I want it to be su­per chea­p.

How does Al­va help pro­vi­de this su­pe­r-­cheap blog hos­tin­g?

  1. It nee­­ds to sca­­le fo­­­llo­­wing the nu­m­­ber of edi­­ts not views.

  2. If it ge­­ts too busy wi­­th edi­­ts, chan­­ges take lo­n­­ger to appea­­r, but the si­­te itself does­n't get any slo­­we­­r.

  3. Edi­­ting and se­r­­ving can be pro­­­vi­­ded by se­­pa­­ra­­te se­r­­vi­­ce­s, so I can use so­­­me su­­pe­­r-­­fast sta­­tic fi­­le se­r­­ver and a su­­pe­­r-­­co­n­­fi­­gu­­ra­­ble WS­­GI de­­plo­­­y­­men­­t.

  4. In­­di­­vi­­dual pa­­ges can be hea­­vi­­ly op­­ti­­mi­­zed so that they do­­wn­­load fast


One of the gui­ding prin­ci­ples he­re is that to de­li­ver this sort of thin­g, in my spa­re ti­me, the de­ve­lo­p­ment pro­ce­ss nee­ds to be stin­gy wi­th the most li­mited re­sour­ce: me. I can't spend a lot of me he­re. I need to be ca­re­ful and not ove­r-­pro­mi­se.

So, whe­ne­ver the­re was a 3r­d-­par­ty tool that saves a sig­ni­fi­cant amount of ti­me, tha­t's what I am usin­g.


Be­cau­se it has a mu­ch stron­ger 3r­d-­par­ty tool­set than Fla­sk or any mi­cro­-­fra­mewo­rk. For exam­ple, the Fla­sk equi­va­lent of djan­go­-a­llau­th bro­ke my wi­ll to li­ve. Be­cau­se the ad­min in­ter­fa­ce means I can start adding da­ta to see if it makes sen­se be­fo­re I wri­te all the re­qui­red views.


Be­cau­se I do­n't want you to ha­ve to crea­te ac­coun­ts he­re un­le­ss you want to, this pro­vi­des (op­tio­nal) so­cial lo­gin and re­gis­tra­tio­n. This was ea­sy to se­tup and wo­rks al­m­sost ou­t-o­f-­the-­box

Bootstrap and Django-bootstrap-toolkit

Niko­la is al­ready hea­vi­ly in­ves­ted in boots­tra­p, so it just ma­de sen­se to go fur­ther do­wn that road. I un­ders­tand boots­tra­p, and djan­go­-­boos­tra­p-­toolkit is ea­sy enou­gh (al­thou­gh I can't make their date­pi­cker wo­rk)


Be­cau­se fi­gh­ting is bo­rin­g.


Be­cau­se Djan­go­'s me­cha­nis­ms to find tem­pla­tes and sta­tic fi­les are many and con­fu­se me.

Redis + RQ + django-rq

It's crucial for the whole approach to use job queues in order to detach the rendering from the actual Django app. This combination makes job dispatching ridiculously easy, setup is trivial (install everything, start redis, a few lines of config, ./ rqworker and off you go) and they provide a django admin page where I can see the failed jobs, which is awesome.


Be­cau­se it's ea­sy enou­gh, and allo­ws me so­me free­dom ex­plo­ring da­ta or­ga­ni­za­tion in my mo­dels wi­thout co­m­mi­tting to it fo­re­ver or re­crea­ting da­ta­ba­ses wi­th test da­ta all the ti­me.


I wi­ll pro­ba­bly ser­ve the ge­ne­ra­ted si­tes via ga­tling just like my cu­rrent si­tes be­cau­se it has the sim­plest na­med do­main con­fi­gu­ra­tion po­s­si­ble, it's fast and ve­ry li­ght in re­sour­ce usage.


A cool, sim­ple edi­tor wi­th li­ve pre­views that su­ppor­ts al­most eve­ry ma­rku­p. Not WY­SIW­YG or even WY­SIW­YM so po­s­si­bly I wi­ll ha­ve to add an al­ter­na­ti­ve. I started using djan­go­-­ma­rki­tup but it's not a good idea (it uses a old ver­sion of ma­rki­tup whi­ch re­qui­res JQue­ry < 1.9) and am in the pro­ce­ss of just using Ma­rki­tup ma­nua­ll­y.

So, feel free to gi­ve Al­va a try an­d/or gi­ve me a han­d, co­m­men­ts wel­co­me.

Here's a very big gun, there's your foot: PHP support in Nikola

I am a ve­ry big pro­po­nent of sta­tic si­te ge­ne­ra­tor­s. I would not ha­ve bo­the­red wri­ting Niko­la othe­rwi­se. But the­re is alwa­ys that fee­ling that ma­y­be the­re is so­me li­ttle thing whi­ch is hard to im­ple­men­t, like a con­tact for­m.

And le­t's fa­ce it, the ea­siest way to sol­ve so­me of tho­se things is by sti­cking a few li­nes of PHP in your HT­M­L.

So, if you rea­lly want to, you can do it. I thi­nk Niko­la (gi­thub mas­te­r) is the first sta­tic si­te ge­ne­ra­tor that su­ppor­ts php co­de. He­re's ho­w:

  1. Add php to your pa­ge_­com­pi­lers (be­cau­se I wi­ll ne­ver put it the­re by de­faul­t):

    post_compilers = {
        "rest": ('.txt', '.rst'),
        "markdown": ('.md', '.mdown', '.markdown'),
        "textile": ('.textile',),
        "txt2tags": ('.t2t',),
        "bbcode": ('.bb',),
        "wiki": ('.wiki',),
        "ipynb": ('.ipynb',),
        "html": ('.html', '.htm'),
        "php": ('.php'),
  2. Add php pos­ts or pa­ges to your pos­t_­pa­ges:

    post_pages = (
        ("posts/*.txt", "posts", "post.tmpl", True),
        ("posts/*.php", "posts", "post.tmpl", True),
        ("stories/*.txt", "stories", "story.tmpl", False),
        ("stories/*.php", "stories", "story.tmpl", False),
  3. Crea­te a php pos­t:

    nikola new_post posts/foo.php
  4. Put php in the­re:

    .. date: 2013/04/16 09:57:09
    .. title: php test
    .. slug: foo
    Print "Hello, World!";

Build the si­te as usua­l, and you should end up wi­th a pa­ge wi­th PHP ex­ten­sio­n, that has that PHP in the "con­ten­t" area, so it wi­ll fo­llow your si­te's the­me. Of cour­se you can't do things like add HTTP hea­ders and su­ch, but he­y, read the ti­tle.

Nikola version 5.4.4 is out!

Ye­s, ver­sion 5.4.4 of Niko­la, my sta­tic si­te/­blog ge­ne­ra­tor is just pu­blis­hed at the usual pla­ce, in­clu­ding the fo­llo­wing im­pro­ve­men­ts:


  • New Ja­­pa­­ne­­se tran­s­­la­­tio­­n.

  • Niko­­­la che­­ck exis­­ts wi­­th 1 if the­­re is an error

  • New HI­­DE_UN­­TRAN­S­­LA­TE­­D_­­PO­S­­TS op­­tion that en­­su­­res you do­­n't ha­­ve mixe­­d-­­lan­­gua­­ge pa­­ges (Is­­sue #373)

  • New the­­me "si­­te-­­pla­­ne­­toi­­d" for use wi­­th the pla­­ne­­toid plu­­gi­n.

  • New 're­­ti­­re­­d' tag for po­s­­ts that should no lo­n­­ger be in fee­­d­s.


  • Added post da­­ta as a up­­to­­­da­­te che­­ck for mus­­ta­­che (Is­­sue #456)

  • Re­­build post pa­­ges when the po­s­­t's tran­s­­la­­tion list chan­­ges (Is­­sue #458)

  • Han­d­­le "-h" (Is­­sue #460)

  • Added co­­­rrect help for co­n­­so­­­le co­­­m­­mand (Is­­sue #460)

  • Es­­ca­­pe twi­­tte­r­­card da­­ta (Is­­sue #452)

  • Added mis­­sing "twi­­tte­r­­ca­r­­d" in sto­­­ry te­m­­pla­­te

  • Added su­­pport for pe­­r-­­lan­­gua­­ge tags (Is­­sue #450)

  • Fix wrong pa­­th spli­­tting (Is­­sue #434)

  • Re­­me­m­­ber lo­­­ca­­le even when se­­t_­­lo­­­ca­­le fai­­les (Is­­sue #446)

  • De­­co­­­de pa­­th ar­­gu­­ment in new_­­post (Is­­sue #442)

  • ta­sk_i­n­­dexes had mis­­sing co­n­­fig de­­pen­­den­­cies (Is­­sue #441)

  • Re­­mo­­­ved bo­­­gus li­nks to sli­­des asse­­ts that we­­re re­­mo­­­ved

  • Co­m­­press­ed fi­­les we­­re seen as unk­­no­­wn by "niko­­­la che­­ck"

  • lo­­­cal sea­r­­ch and mus­­ta­­che plu­­gins must be di­s­a­­bled by de­­fault (Is­­sue #437)

  • Avoid fai­­lu­­re if the­­re are no tags and USE_­G­­ZIP is ena­­bled (Is­­sue #439)

  • Fix as­­pect ra­­tio de­­te­c­­tion in Vi­­meo vi­­deos (Is­­sue #440)

  • Blo­­­gger im­­po­r­­ter was pa­s­­sing wrong op­­tions to "niko­­­la ini­­t" (Is­­sue #408)

Contents © 2000-2024 Roberto Alsina