Passing variables to view

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Passing variables to view

Germán Rimoldi
Hi Guys! 

I'm getting started with CherryPy. I'm finding the learning curve pretty steep for a newbie :(

I have a very simple question: How do I pass variables to an html view?

class Root: 
    @cherrypy.expose
    def index(self): 
        c = cherrypy.thread_data.db.cursor() 
        c.execute('select count(*) from Website') 
        res = c.fetchone() 
        c.close() 
        open("index.html") 
cherrypy.quickstart(Root())

My index.html, being:
<html>
    <head></head>
    <body>
    Hello, you have %d records in your table %res[0]  
    </body>
 </html>

How should I rewrite my index function, to render the variables to my html file?

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing variables to view

Ralph Heinkel-2

Hi Germán,

your index function could look like this:

    @cherrypy.expose
    def index(self): 
        c = cherrypy.thread_data.db.cursor() 
        c.execute('select count(*) from Website') 
        res = c.fetchone() 
        c.close() 
        html = open("index.html").read()
        return html % res[0]

While your index.html is just:

<html>
    <head></head>
    <body>
    Hello, you have %d records in your table. 
    </body>
</html>

However I would reccomend that you look into something like jinja2 for rendering your templates, make things a lot easier.

Ralph



On 12/18/2016 10:28 PM,  Rimoldi wrote:
Hi Guys! 

I'm getting started with CherryPy. I'm finding the learning curve pretty steep for a newbie :(

I have a very simple question: How do I pass variables to an html view?

class Root: 
    @cherrypy.expose
    def index(self): 
        c = cherrypy.thread_data.db.cursor() 
        c.execute('select count(*) from Website') 
        res = c.fetchone() 
        c.close() 
        open("index.html") 
cherrypy.quickstart(Root())

My index.html, being:
<html>
    <head></head>
    <body>
    Hello, you have %d records in your table %res[0]  
    </body>
 </html>

How should I rewrite my index function, to render the variables to my html file?
--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing variables to view

Germán Rimoldi
In reply to this post by Germán Rimoldi
Much appreciated Raph! 

It works like a charm

:)



On Sunday, December 18, 2016 at 6:39:40 PM UTC-3, Germán Rimoldi wrote:
Hi Guys! 

I'm getting started with CherryPy. I'm finding the learning curve pretty steep for a newbie :(

I have a very simple question: How do I pass variables to an html view?

class Root: 
    @cherrypy.expose
    def index(self): 
        c = cherrypy.thread_data.db.cursor() 
        c.execute('select count(*) from Website') 
        res = c.fetchone() 
        c.close() 
        open("index.html") 
cherrypy.quickstart(Root())

My index.html, being:
<html>
    <head></head>
    <body>
    Hello, you have %d records in your table %res[0]  
    </body>
 </html>

How should I rewrite my index function, to render the variables to my html file?

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Passing variables to view

Stephan Semerad
In reply to this post by Germán Rimoldi
Hi there. 

Best is you learn a templating tool (mako, jinja). I started rendering html in cherrypy at the beginning by doing the following:

import cherrypy

class app():

    @cherrypy.expose
    def index(self):
        number = 20.3
        string = 'hello world!'

        return """
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <title></title>
          </head>
          <body>
            <h1>this is a number: %d</h1>
            <h1>this is a string: %s</h1>
          </body>
        </html>
        """ % (number, string)

cherrypy.quickstart(app(), "/")

but i do not recommend it for larger applications. 


On Sunday, December 18, 2016 at 10:39:40 PM UTC+1, Germán Rimoldi wrote:
Hi Guys! 

I'm getting started with CherryPy. I'm finding the learning curve pretty steep for a newbie :(

I have a very simple question: How do I pass variables to an html view?

class Root: 
    @cherrypy.expose
    def index(self): 
        c = cherrypy.thread_data.db.cursor() 
        c.execute('select count(*) from Website') 
        res = c.fetchone() 
        c.close() 
        open("index.html") 
cherrypy.quickstart(Root())

My index.html, being:
<html>
    <head></head>
    <body>
    Hello, you have %d records in your table %res[0]  
    </body>
 </html>

How should I rewrite my index function, to render the variables to my html file?

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Loading...