librelist archives

« back to archive

pystache flask and loading templates

pystache flask and loading templates

From:
Alessio Civitillo
Date:
2011-12-19 @ 20:47
Hello,

I am trying to use pystache with flask, but I can't read the templates.

Let's say I have and index.html. I try to do the following:

===========================
from flask import Flask
import pystache

app = Flask(__name__)

@app.route("/")
def index():
tmpl=unicode(open("index.html","r").read())
d={"index_text":"This is some index text to see if this works"}
return pystache.render(tmpl,d)

if __name__=="__main__":
app.run()
============================

This is not working. Could somebody help me out? Is there a better way to
load templates? Thanks.

-- 
Regards,
------------------------------------
Alessio Civitillo
alessiocivitillo@gmail.com
Mobile: (0045) 52645608
Linkedin: http://it.linkedin.com/in/alessiocivitillo

Re: [pystache] pystache flask and loading templates

From:
Chris Jerdonek
Date:
2011-12-20 @ 02:01
On Mon, Dec 19, 2011 at 12:47 PM, Alessio Civitillo
<alessiocivitillo@gmail.com> wrote:
> def index():
> tmpl=unicode(open("index.html","r").read())
> d={"index_text":"This is some index text to see if this works"}
> return pystache.render(tmpl,d)
>
> This is not working. Could somebody help me out? Is there a better way to
> load templates? Thanks.

Currently, there are roughly three increasingly more "automatic" ways
of loading templates:

(1) Read the template file yourself and pass it to pystache.render(),
which you are doing,
(2) Read the template using a pystache.Loader and pass the return
value to pystache.render(), or
(3) Load and render the template using a pystache.View().

I'd suggest starting with (1), like you are doing.  What error message
are you getting?  Also, try printing the string "tmpl" for
troubleshooting purposes as well as the return value of index().
Lastly, try hard-coding "tmpl" so you can figure out if the problem is
happening before or after the template is loaded.

--Chris

Re: [pystache] pystache flask and loading templates

From:
Alessio Civitillo
Date:
2011-12-20 @ 10:14
Thanks for helping out. Below as you suggested.


> What error message
> are you getting?


**The error is**: Internal server error.

**Terminal output***:

$ sudo python2.7 flask_wk.py
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [20/Dec/2011 11:06:52] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [20/Dec/2011 11:06:54] "GET /favicon.ico HTTP/1.1" 404 -


**Output of tmpl:**

$ sudo python2.7 flask_wk.py
 * Running on http://127.0.0.1:5000/
<head>
        <link rel="stylesheet" href="screen.css" type="text/css"
media="screen">
</head>
<body>
<div id="container">
        <div id="header">
                <h1>
                        Site name
                </h1>
        </div>
        <div id="navigation">
                <ul>
                        <li><a href="#">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Contact us</a></li>
                </ul>
        </div>
        <div id="content">
                <h2>
                        Page heading
                </h2>
                <p>
                {{index_text}}
                </p>
        </div>
        <div id="footer">
                Copyright © Site name, 20XX
        </div>
</div>
</body>

127.0.0.1 - - [20/Dec/2011 11:12:52] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [20/Dec/2011 11:12:52] "GET /favicon.ico HTTP/1.1" 404 -



Currently, there are roughly three increasingly more "automatic" ways
> of loading templates:
> (1) Read the template file yourself and pass it to pystache.render(),
> which you are doing,
> (2) Read the template using a pystache.Loader and pass the return
> value to pystache.render(), or
> (3) Load and render the template using a pystache.View().
> I'd suggest starting with (1), like you are doing.  What error message
> are you getting?  Also, try printing the string "tmpl" for
> troubleshooting purposes as well as the return value of index().
> Lastly, try hard-coding "tmpl" so you can figure out if the problem is
> happening before or after the template is loaded.




On Tue, Dec 20, 2011 at 3:01 AM, Chris Jerdonek <chris.jerdonek@gmail.com>wrote:

> On Mon, Dec 19, 2011 at 12:47 PM, Alessio Civitillo
> <alessiocivitillo@gmail.com> wrote:
> > def index():
> > tmpl=unicode(open("index.html","r").read())
> > d={"index_text":"This is some index text to see if this works"}
> > return pystache.render(tmpl,d)
> >
> > This is not working. Could somebody help me out? Is there a better way to
> > load templates? Thanks.
>
> Currently, there are roughly three increasingly more "automatic" ways
> of loading templates:
>
> (1) Read the template file yourself and pass it to pystache.render(),
> which you are doing,
> (2) Read the template using a pystache.Loader and pass the return
> value to pystache.render(), or
> (3) Load and render the template using a pystache.View().
>
> I'd suggest starting with (1), like you are doing.  What error message
> are you getting?  Also, try printing the string "tmpl" for
> troubleshooting purposes as well as the return value of index().
> Lastly, try hard-coding "tmpl" so you can figure out if the problem is
> happening before or after the template is loaded.
>
> --Chris
>



-- 
Regards,
------------------------------------
Alessio Civitillo
alessiocivitillo@gmail.com
Mobile: (0045) 52645608
Linkedin: http://it.linkedin.com/in/alessiocivitillo

Re: [pystache] pystache flask and loading templates

From:
Alessio Civitillo
Date:
2011-12-20 @ 10:56
On Tue, Dec 20, 2011 at 11:14 AM, Alessio Civitillo <
alessiocivitillo@gmail.com> wrote:

> Thanks for helping out. Below as you suggested.
>
>
>> What error message
>> are you getting?
>
>
> **The error is**: Internal server error.
>
> **Terminal output***:
>
> $ sudo python2.7 flask_wk.py
>  * Running on http://127.0.0.1:5000/
> 127.0.0.1 - - [20/Dec/2011 11:06:52] "GET / HTTP/1.1" 500 -
> 127.0.0.1 - - [20/Dec/2011 11:06:54] "GET /favicon.ico HTTP/1.1" 404 -
>
>
> **Output of tmpl:**
>
> $ sudo python2.7 flask_wk.py
>  * Running on http://127.0.0.1:5000/
> <head>
>         <link rel="stylesheet" href="screen.css" type="text/css"
> media="screen">
> </head>
> <body>
> <div id="container">
>         <div id="header">
>                 <h1>
>                         Site name
>                 </h1>
>         </div>
>         <div id="navigation">
>                 <ul>
>                         <li><a href="#">Home</a></li>
>                         <li><a href="#">About</a></li>
>                         <li><a href="#">Services</a></li>
>                         <li><a href="#">Contact us</a></li>
>                 </ul>
>         </div>
>         <div id="content">
>                 <h2>
>                         Page heading
>                 </h2>
>                 <p>
>                 {{index_text}}
>                 </p>
>         </div>
>         <div id="footer">
>                 Copyright © Site name, 20XX
>         </div>
> </div>
> </body>
>
> 127.0.0.1 - - [20/Dec/2011 11:12:52] "GET / HTTP/1.1" 200 -
> 127.0.0.1 - - [20/Dec/2011 11:12:52] "GET /favicon.ico HTTP/1.1" 404 -
>
>
>
> Currently, there are roughly three increasingly more "automatic" ways
>> of loading templates:
>> (1) Read the template file yourself and pass it to pystache.render(),
>> which you are doing,
>> (2) Read the template using a pystache.Loader and pass the return
>> value to pystache.render(), or
>> (3) Load and render the template using a pystache.View().
>> I'd suggest starting with (1), like you are doing.  What error message
>> are you getting?  Also, try printing the string "tmpl" for
>> troubleshooting purposes as well as the return value of index().
>> Lastly, try hard-coding "tmpl" so you can figure out if the problem is
>> happening before or after the template is loaded.
>
>
>
>

Ok, I have found the problem:

*** UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
586: ordinal not in range(128) ***

Re: [pystache] pystache flask and loading templates

From:
Chris Jerdonek
Date:
2011-12-20 @ 13:39
On Tue, Dec 20, 2011 at 2:56 AM, Alessio Civitillo
<alessiocivitillo@gmail.com> wrote:
> Ok, I have found the problem:
>
> *** UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
> 586: ordinal not in range(128) ***

Great.  So do you know how to resolve that error, or do you still need help?

--Chris