librelist archives

« back to archive

Re: [flask] js and flask templating

Re: [flask] js and flask templating

From:
Samuel Čavoj
Date:
2015-04-07 @ 09:27
Not sure whether I'm missing something, but have you tried the default 
preinstalled templating engine - Jinja2? http://jinja.pocoo.org/docs/dev/

Regarding i18n, I do not have much experience with that, but you could try
Flask-Babel. https://pythonhosted.org/Flask-Babel/

On Apr 7, 2015 00:08, Ivan Sergio Borgonovo <mail@webthatworks.it> wrote:
>
> I'm looking for a templating system that works nice with js and 
> python/flask. 
>
> An obvious candidate could be mustache (https://mustache.github.io/) 
>
> And I've found: 
> https://github.com/mattupstate/flask-stache/ 
> https://github.com/ahri/flask-mustache 
>
> any other candidate? 
>
> Next step would be to find some tool to share i18n efforts between 
> python and js (mainly translation but also dates, numbers... formatting). 
>
> Does anyone has any thought to share about this class of problems? 
>
> I'd like to share as much work as possible between a Phonegap app[1] and 
> its web counterpart dealing as few as possible with JS and node. 
>
> thanks 
>
> [1] or any Open equivalent 
>
> -- 
> Ivan Sergio Borgonovo 
> http://www.webthatworks.it 
>

Re: [flask] js and flask templating

From:
Ivan Sergio Borgonovo
Date:
2015-04-07 @ 11:20
On 04/07/2015 11:27 AM, Samuel Čavoj wrote:
> Not sure whether I'm missing something, but have you tried the default 
preinstalled templating engine - Jinja2? http://jinja.pocoo.org/docs/dev/

Jinja for loop:
{% for item in list %}
{% endfor %}

while eg. Handlebars
{{#each list}}
{{/each}}

There is an implementation of mustache for js and python, so I could
simply use the same exact template and render it in the browser (or
PhoneGap app) and in the server (flask).
I didn't understand yet if there is a way to manage template
nesting/inheritance.

I was wondering if a) anyone has some experience with mustache and flask
(I saw there are a couple of extensions) and b) if any alternative
approach that works with flask.

I've the same problem with translations.
I'd prefer to use the same machinery to translate stuff that will run in
python and js.
gettext is reasonably "cross language".
Babel is gettext based so it could be fine.
Extracting and translating strings seems nearly feasible "cross language".

You generally extract strings, translate them, compile them to .mo files.

js doesn't seems to have any library I'm aware of that can directly load
.mo files. JS tools generally output js or json inspite of .mo.
This somehow is a minor problem provided that once the language specific
gettext initialization is set up at least using translations will have a
similar syntax across languages. I don't expect surprises here, gettext
functions are gettext functions.

Furthermore completely separate templates from translations is going to
make designer life harder since they won't be able to put any actual
text in the template, just {{tags}}.

Since mustache doesn't support function calls I can't write templates like:

{{ _('Some string to be translated') }}

I've to pass the tag to the template already translated.

So a template may end up being filled with unfriendly:

{{ greeting1 }}
{{ greeting2 }}
{{ subtotalmessage1 }}
{{ subtotalmessage2 }}

> Regarding i18n, I do not have much experience with that, but you could 
try Flask-Babel. https://pythonhosted.org/Flask-Babel/
> 
> On Apr 7, 2015 00:08, Ivan Sergio Borgonovo <mail@webthatworks.it> wrote:
>>
>> I'm looking for a templating system that works nice with js and 
>> python/flask. 
>>
>> An obvious candidate could be mustache (https://mustache.github.io/) 
>>
>> And I've found: 
>> https://github.com/mattupstate/flask-stache/ 
>> https://github.com/ahri/flask-mustache 
>>
>> any other candidate? 
>>
>> Next step would be to find some tool to share i18n efforts between 
>> python and js (mainly translation but also dates, numbers... formatting). 
>>
>> Does anyone has any thought to share about this class of problems? 
>>
>> I'd like to share as much work as possible between a Phonegap app[1] and 
>> its web counterpart dealing as few as possible with JS and node. 
>>
>> thanks 
>>
>> [1] or any Open equivalent 
>>
>> -- 
>> Ivan Sergio Borgonovo 
>> http://www.webthatworks.it 
>>

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it

Re: [flask] js and flask templating

From:
Felipe Barreto Volpone
Date:
2015-04-07 @ 11:57
Hello Ivan,

you could use an REST API (with Flask) with a client in html+js. It's a
good design patern have a client layer with less complexity. Furthermore,
if you choose this kind of organization your PhoneGap app can use a
different architecture. As an example:
 - In browser: jQuery+css+html+mustache.js
 - In PhoneGap: angular+html+css

 Anyway, you can find a lot of examples to create this kind of application.
If you still've some doubt, please contact us.

Tks

2015-04-07 8:20 GMT-03:00 Ivan Sergio Borgonovo <mail@webthatworks.it>:

> On 04/07/2015 11:27 AM, Samuel Čavoj wrote:
> > Not sure whether I'm missing something, but have you tried the default
> preinstalled templating engine - Jinja2? http://jinja.pocoo.org/docs/dev/
>
> Jinja for loop:
> {% for item in list %}
> {% endfor %}
>
> while eg. Handlebars
> {{#each list}}
> {{/each}}
>
> There is an implementation of mustache for js and python, so I could
> simply use the same exact template and render it in the browser (or
> PhoneGap app) and in the server (flask).
> I didn't understand yet if there is a way to manage template
> nesting/inheritance.
>
> I was wondering if a) anyone has some experience with mustache and flask
> (I saw there are a couple of extensions) and b) if any alternative
> approach that works with flask.
>
> I've the same problem with translations.
> I'd prefer to use the same machinery to translate stuff that will run in
> python and js.
> gettext is reasonably "cross language".
> Babel is gettext based so it could be fine.
> Extracting and translating strings seems nearly feasible "cross language".
>
> You generally extract strings, translate them, compile them to .mo files.
>
> js doesn't seems to have any library I'm aware of that can directly load
> .mo files. JS tools generally output js or json inspite of .mo.
> This somehow is a minor problem provided that once the language specific
> gettext initialization is set up at least using translations will have a
> similar syntax across languages. I don't expect surprises here, gettext
> functions are gettext functions.
>
> Furthermore completely separate templates from translations is going to
> make designer life harder since they won't be able to put any actual
> text in the template, just {{tags}}.
>
> Since mustache doesn't support function calls I can't write templates like:
>
> {{ _('Some string to be translated') }}
>
> I've to pass the tag to the template already translated.
>
> So a template may end up being filled with unfriendly:
>
> {{ greeting1 }}
> {{ greeting2 }}
> {{ subtotalmessage1 }}
> {{ subtotalmessage2 }}
>
> > Regarding i18n, I do not have much experience with that, but you could
> try Flask-Babel. https://pythonhosted.org/Flask-Babel/
> >
> > On Apr 7, 2015 00:08, Ivan Sergio Borgonovo <mail@webthatworks.it>
> wrote:
> >>
> >> I'm looking for a templating system that works nice with js and
> >> python/flask.
> >>
> >> An obvious candidate could be mustache (https://mustache.github.io/)
> >>
> >> And I've found:
> >> https://github.com/mattupstate/flask-stache/
> >> https://github.com/ahri/flask-mustache
> >>
> >> any other candidate?
> >>
> >> Next step would be to find some tool to share i18n efforts between
> >> python and js (mainly translation but also dates, numbers...
> formatting).
> >>
> >> Does anyone has any thought to share about this class of problems?
> >>
> >> I'd like to share as much work as possible between a Phonegap app[1] and
> >> its web counterpart dealing as few as possible with JS and node.
> >>
> >> thanks
> >>
> >> [1] or any Open equivalent
> >>
> >> --
> >> Ivan Sergio Borgonovo
> >> http://www.webthatworks.it
> >>
>
> --
> Ivan Sergio Borgonovo
> http://www.webthatworks.it
>
>

Re: [flask] js and flask templating

From:
Ivan Sergio Borgonovo
Date:
2015-04-07 @ 13:05
On 04/07/2015 01:57 PM, Felipe Barreto Volpone wrote:
> Hello Ivan,
> 
> you could use an REST API (with Flask) with a client in html+js. It's a
> good design patern have a client layer with less complexity.
> Furthermore, if you choose this kind of organization your PhoneGap app
> can use a different architecture. As an example:
>  - In browser: jQuery+css+html+mustache.js
>  - In PhoneGap: angular+html+css 

The problem is I want to render as much as possible on the server side
for the first request.
So eg. just using angular on both the webapp and the "browser" and
pushing all the content later with JSON won't work for me.

It is easier to put all the pieces together with node.js but then I'll
have to deal with js and js development and deployment environment more
than I'm willing to.

I knew js was a bad thing back in the '90s.

>  Anyway, you can find a lot of examples to create this kind of
> application. If you still've some doubt, please contact us.
> 
> Tks
> 
> 2015-04-07 8:20 GMT-03:00 Ivan Sergio Borgonovo <mail@webthatworks.it
> <mailto:mail@webthatworks.it>>:
> 
>     On 04/07/2015 11:27 AM, Samuel Čavoj wrote:
>     > Not sure whether I'm missing something, but have you tried the 
default preinstalled templating engine - Jinja2? 
http://jinja.pocoo.org/docs/dev/
> 
>     Jinja for loop:
>     {% for item in list %}
>     {% endfor %}
> 
>     while eg. Handlebars
>     {{#each list}}
>     {{/each}}
> 
>     There is an implementation of mustache for js and python, so I could
>     simply use the same exact template and render it in the browser (or
>     PhoneGap app) and in the server (flask).
>     I didn't understand yet if there is a way to manage template
>     nesting/inheritance.
> 
>     I was wondering if a) anyone has some experience with mustache and flask
>     (I saw there are a couple of extensions) and b) if any alternative
>     approach that works with flask.
> 
>     I've the same problem with translations.
>     I'd prefer to use the same machinery to translate stuff that will run in
>     python and js.
>     gettext is reasonably "cross language".
>     Babel is gettext based so it could be fine.
>     Extracting and translating strings seems nearly feasible "cross
>     language".
> 
>     You generally extract strings, translate them, compile them to .mo
>     files.
> 
>     js doesn't seems to have any library I'm aware of that can directly load
>     .mo files. JS tools generally output js or json inspite of .mo.
>     This somehow is a minor problem provided that once the language specific
>     gettext initialization is set up at least using translations will have a
>     similar syntax across languages. I don't expect surprises here, gettext
>     functions are gettext functions.
> 
>     Furthermore completely separate templates from translations is going to
>     make designer life harder since they won't be able to put any actual
>     text in the template, just {{tags}}.
> 
>     Since mustache doesn't support function calls I can't write
>     templates like:
> 
>     {{ _('Some string to be translated') }}
> 
>     I've to pass the tag to the template already translated.
> 
>     So a template may end up being filled with unfriendly:
> 
>     {{ greeting1 }}
>     {{ greeting2 }}
>     {{ subtotalmessage1 }}
>     {{ subtotalmessage2 }}
> 
>     > Regarding i18n, I do not have much experience with that, but you
>     could try Flask-Babel. https://pythonhosted.org/Flask-Babel/
>     >
>     > On Apr 7, 2015 00:08, Ivan Sergio Borgonovo <mail@webthatworks.it
>     <mailto:mail@webthatworks.it>> wrote:
>     >>
>     >> I'm looking for a templating system that works nice with js and
>     >> python/flask.
>     >>
>     >> An obvious candidate could be mustache (https://mustache.github.io/)
>     >>
>     >> And I've found:
>     >> https://github.com/mattupstate/flask-stache/
>     >> https://github.com/ahri/flask-mustache
>     >>
>     >> any other candidate?
>     >>
>     >> Next step would be to find some tool to share i18n efforts between
>     >> python and js (mainly translation but also dates, numbers...
>     formatting).
>     >>
>     >> Does anyone has any thought to share about this class of problems?
>     >>
>     >> I'd like to share as much work as possible between a Phonegap
>     app[1] and
>     >> its web counterpart dealing as few as possible with JS and node.
>     >>
>     >> thanks
>     >>
>     >> [1] or any Open equivalent
>     >>
>     >> --
>     >> Ivan Sergio Borgonovo
>     >> http://www.webthatworks.it
>     >>
> 
>     --
>     Ivan Sergio Borgonovo
>     http://www.webthatworks.it
> 
> 

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it