Quantcast

Trying to build a fully synthesized site

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

Trying to build a fully synthesized site

jerry
Hi,
I am completely new to CherryPy and am trying to do something different than a typical web site.
I am using http to expose data that has be learned within a local management tool. My config file allows the user to define a page URL and what data is returned in a json dictionary. They can also specify a template file (jinja2) and I will feed the data through the template to return them the rendered result.

So there are no specific pages that exist, so the define a class or function for a page as in the examples doesn't seem to make any sense. I want to tell cherrypy which pages exist and then route them all through a single routine that looks up the page information and returns json or html. What is the best way to do this? This is my whole reason for embedding the server.

I might think this could be a good addition to the FAQ, I can't be the only person who would want to do this.

thanks,
jerry


--
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: Trying to build a fully synthesized site

Joseph S. Tate
Your description of the problem isn't helping me figure out what you want to do. Can you give some concrete examples?

There are lots of tool examples that show how to render templates, for example: http://tools.cherrypy.org/wiki/Jinja

It sounds like you probably want to build a custom dispatcher. When a request comes in, it gets sent to the appropriate dispatcher (which you can set up in the config file). The default dispatcher maps the URL to a class tree, for example. CherryPy ships with a few dispatchers. One of them may work out of the box, or you may want to look at the code for these and how you can modify them for your own use. https://cherrypy.readthedocs.io/en/3.3.0/tutorial/dispatching.html

You may also want to look at the "default" method, which allows you to set up a single root class, and have it handle all requests. The extra path info is passed as positional parameters. It's described on the dispatching page above. You can also write a _cp_dispatch method in your mounted class that will receive the remainder of the path as a single parameter. http://docs.cherrypy.org/en/latest/advanced.html#the-special-cp-dispatch-method

Joseph

On Wed, Oct 26, 2016 at 5:00 PM <[hidden email]> wrote:
Hi,
I am completely new to CherryPy and am trying to do something different than a typical web site.
I am using http to expose data that has be learned within a local management tool. My config file allows the user to define a page URL and what data is returned in a json dictionary. They can also specify a template file (jinja2) and I will feed the data through the template to return them the rendered result.

So there are no specific pages that exist, so the define a class or function for a page as in the examples doesn't seem to make any sense. I want to tell cherrypy which pages exist and then route them all through a single routine that looks up the page information and returns json or html. What is the best way to do this? This is my whole reason for embedding the server.

I might think this could be a good addition to the FAQ, I can't be the only person who would want to do this.

thanks,
jerry


--
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: Trying to build a fully synthesized site

Tim Roberts
In reply to this post by jerry
[hidden email] wrote:

>
> I am completely new to CherryPy and am trying to do something
> different than a typical web site.
> I am using http to expose data that has be learned within a local
> management tool. My config file allows the user to define a page URL
> and what data is returned in a json dictionary. They can also specify
> a template file (jinja2) and I will feed the data through the template
> to return them the rendered result.
>
> So there are no specific pages that exist, so the define a class or
> function for a page as in the examples doesn't seem to make any sense.
> I want to tell cherrypy which pages exist and then route them all
> through a single routine that looks up the page information and
> returns json or html. What is the best way to do this? This is my
> whole reason for embedding the server.

At some top level, you must have some common URL root that identifies
your service.  For example:
    http://mysite.com/localmgmt/other/data?additional=data
    http://mysite.com/localmgmt/some/pages?other=info

If everything starts with "localmgmt", then that's what you need to
mount in your CherryPy tree.  The rest of the URL arrives in
cherrypy.request.path_info, and you can parse that in whatever way you'd
like.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
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: Trying to build a fully synthesized site

jerry
In reply to this post by jerry
Here's more info and clearer questions:
I have a config that loads a set of pages and tells which variables need to be extracted from the memory state to satisfy the query.

Here's what I am trying to do:
Build up something that knows which pages are defined and send 404s for things not defined. If cherrypy can do that, it would be great...
have a generic class/function that all the defines queries get funneled to:
   That wants to get  instance of the page description class that was loaded from the config
   it looks at the page that was requested, gets the list of data that was desired and builds up the response json
   if there was a jinja template specified, feed the data into the template ann capture the rendered output
   send the response back

Here are some specific questions:
  can I build the dispatcher at runtime?
  is it worth doing this or should I generate the 404s from my class/function?
  if I don't do a dispatcher, how do I say that everything below a point on the path is handled by the class/function?
  How do I pass the instance of the page description class and other things into class/function?
  I would assume a class is easier since it then can have private storage. I am thinking right about this?
  
thanks in advance,
jerry

On Wednesday, October 26, 2016 at 2:00:18 PM UTC-7, [hidden email] wrote:
Hi,
I am completely new to CherryPy and am trying to do something different than a typical web site.
I am using http to expose data that has be learned within a local management tool. My config file allows the user to define a page URL and what data is returned in a json dictionary. They can also specify a template file (jinja2) and I will feed the data through the template to return them the rendered result.

So there are no specific pages that exist, so the define a class or function for a page as in the examples doesn't seem to make any sense. I want to tell cherrypy which pages exist and then route them all through a single routine that looks up the page information and returns json or html. What is the best way to do this? This is my whole reason for embedding the server.

I might think this could be a good addition to the FAQ, I can't be the only person who would want to do this.

thanks,
jerry


--
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: Trying to build a fully synthesized site

Joseph S. Tate


On Fri, Oct 28, 2016 at 8:44 PM <[hidden email]> wrote:
Here's more info and clearer questions:
I have a config that loads a set of pages and tells which variables need to be extracted from the memory state to satisfy the query.

Here's what I am trying to do:
Build up something that knows which pages are defined and send 404s for things not defined. If cherrypy can do that, it would be great...
have a generic class/function that all the defines queries get funneled to:
   That wants to get  instance of the page description class that was loaded from the config
   it looks at the page that was requested, gets the list of data that was desired and builds up the response json
   if there was a jinja template specified, feed the data into the template ann capture the rendered output
   send the response back

How do you define the pages? How do your urls map to those pages? If you can describe this, you can create a dispatcher.

If you want a CMS, you should really just use one. If you want to serve files, just use the static file tool/handler. If you want to run code based on static files, maybe you should start with the static file tools and modify them to fit your needs.
 

Here are some specific questions:
  can I build the dispatcher at runtime?

You build it at server start time, not at request time. 

  is it worth doing this or should I generate the 404s from my class/function?

raise cherrypy.NotFound, but the default dispatcher already does this for any url not mapped to a handler.
 
  if I don't do a dispatcher, how do I say that everything below a point on the path is handled by the class/function?

cherrypy.tree.mount() You should read the documentation for this anyway. cherrypy.quickstart is just the beginning, and anything more complicated than a single class root will need this. http://docs.cherrypy.org/en/latest/basics.html#multiple-applications

As Tim said, that's handled for you in the default dispatcher. If the dispatcher walks a tree and finds a callable, it passes everything left in the url (query params as kwargs, path segments as positional args) to that method. Here's an example:

import cherrypy

class Root():
   @cherrypy.expose
   def foo(s, *a, **kw):
       return cherrypy.request.path_info + str(a) + str(kw)

cherrypy.tree.mount(Root(), '/absalom', {})
cherrypy.engine.start()
cherrypy.engine.block()

### Requesting http://localhost:8080/absalom/foo/bar/baz?a=arg&b=blarg returns
### /foo/bar/baz('bar', 'baz'){'a': u'arg', 'b': u'blarg'}

  How do I pass the instance of the page description class and other things into class/function?

Classes are instantiated at start time and kept in RAM in thread-unsafe memory. Pass anything you want into __init__() method for the class before mounting or quickstarting. Otherwise, just define everything in the cherrypy ini file and keep everything in cherrypy.config. You can also store information in the request object by injecting it with tools at various injection points, or on the server engine using engine plugins, but I doubt you'll need to do that.
 
  I would assume a class is easier since it then can have private storage. I am thinking right about this?

yes.

--
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...