Set up a session when your classes are in a separate files

74 views
Skip to first unread message

Adam Bengtsson

unread,
Aug 15, 2012, 10:36:15 AM8/15/12
to we...@googlegroups.com
Hello agian,

I was eager to try out the session functionality in web.py but am not really sure how to go about doing this. Having consulted the cookbook I've found the Working with Sessions-example.

In this example, the app-object is passed as an argument to the session initialization, and the session-object is then used globally by the other classes _in this file_.

How would I go about if I wanted to initialize or at least use a session object in a different file where I no longer have the app-reference? Is there some obscure reference to the current application somewhere within the web-package?

Thank you,
Adam

Shannon Cruey

unread,
Aug 15, 2012, 2:07:57 PM8/15/12
to we...@googlegroups.com
I had this issue when I started with web.py, and I was completely new to Python too.

Right or wrong, here's what I did.  I moved on because it worked.

1) I created a file called sharedsession.py  It's one line - looks like this:
  • session = None
2) in my main web.py file I do this:
  • import sharedsession
  • ...
  • sharedsession.session = web.session.Session(app, web.session.ShelfStore(shelve.open('session.shelf')))

(I'm using shelf because I was having problems with the other type and wsgi/apache, and I don't wanna use a db.)

Then, in all my other modules I "import sharedsession" and just reference 'session' there.

Like I said, I'm no python expert, in fact I still don't fully understand "import" to this day.  (It's like a black box for me... it works, it's magical in some ways, and I don't understand why.)

If this is horribly wrong I hope someone else will warn you not to use my terrible idea.  If it's right, you're welcome! ;-)




--
You received this message because you are subscribed to the Google Groups "web.py" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/GyuekdV6G9EJ.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

Marios Zindilis

unread,
Aug 15, 2012, 3:05:27 PM8/15/12
to we...@googlegroups.com
Perhaps this covers your question?

--
Marios Zindilis

Adam Bengtsson

unread,
Aug 16, 2012, 3:43:52 PM8/16/12
to we...@googlegroups.com
Hello,

Thank you both for your replies, having totally missed the cookbook example, I think I'll give that a try!

//Adam
Reply all
Reply to author
Forward
0 new messages