Persistent python object and the Web

Bengt Richter bokr at oz.net
Fri Apr 22 13:31:25 EDT 2005


On 21 Apr 2005 11:15:02 -0700, ramon.aragues at gmail.com wrote:

>Hi,
>
>I'd like to know if I am trying to do something impossible or I am just
>being unable to find how to do it. In the latter case, please... help
>me...
>
>I=B4ve implemented a python class Graph, which handles graphs (with its
>nodes, edges, finding paths, etc). In my text-menu interface, I can add
>nodes, delete nodes, add edges, find shortest routes, etc. I simply
>create a graph_object before presenting the text menu and then, when a
>user choses a command (eg. add-node) I call the method of this object
>with the appropiate parameters.
>
How much total data is involved in the state of the Graph object?
If it's typed in, it can't be that much? Or do you automatically generate
huge subtrees etc.? Anyway, the amount will influence your options.

Then the question is where you want the state to persist. A small amount
can be carried right in the url. Or in preset values in forms, which can
have hidden elements, or in javascript variables that are set from script
parts that represent the state, or cookies that can contain references to
data stored one way or another on the server.

>Now... I need to "translate" this text-menu interface  to a web
>interface. The same as the text-menu interface, but letting the user do
>it through the web. I have created an HTML that lets the user enter the
>parameters and chose which command to execute by pushing different
>buttons. But...
You could conceivably implement the whole thing via javascript in an html
page that runs entirely in the client browser, but that probably wouldn't
be as much fun as python ;-)

>
>My problem is that I don't know how to create a graph_object that
>remains persistent through time (it has to be the same graph_object for
>the first command as for the last one "pushed" by the user). I want to
>be able to create the object when the user enters the web page (by
>calling graph_object =3D Graph(id=3D"graph1")) and then make calls to
>graph_object methods when the user pushes a button.
>
>I'm using python2.3, mod_python and Apache.
>
>Any help will be greatly appreaciated.
>
If the state is not too huge, I think I might first create
Graph dump and load methods that would output a nice text
representation of the graph state, and then use a textarea
form element to carry that info, which would get submitted
along with the visible form data. (You could make it all visible
for debugging). You could roll your own format suitable for
textarea transfer. Or you might consider using pickle.Pickler
to create string representations of state info (even a whole tree
if you want to save everything) and maybe do something simple
to make the resulting string ok for a textarea (I don't know
if it can be used as is with protocol 0).
Then you could use pickle.Unpickler to reconstitute what you pickled
when it comes back to the server from the submittal of the textarea
(whose default content you set up each time you serve a web page)
when it comes back again.

Then on the server you can just create an empty Graph instance
an use the submitted textarea state for the load method, and then
go on with the indiviual command data, which would update the Graph
instance state and then you return a web page with the textarea
preset to whatever you get from your graph instance .dump method.

This would be just cgi with no server data base and no cookies or
any of that stuff. Everything persists in the page form data that
is handed back and forth. The client should be able to save the page
to disk  and bring it up again, and if your actions and hrefs are
absolute, they should go back to your server.

Anyway, you should be able to test this theory with a simple page
with a simple form. I may have forgotten some gotcha ;-)

If you have really big data, you might want to have it persist
on the server one way or another.

Regards,
Bengt Richter



More information about the Python-list mailing list