There have been quite some discussions about reloading files that I
haven't really followed because up until recently, reload was working
fine for me. My application is just one code.py file ending with:
if __name__ == "__main__":
web.run(urls, globals(), web.reloader)
Using web.py 0.2, this worked just fine. Since the upgrade to 0.21, I
have to restart the server (built-in as well as lighttpd give the same
results) to make changes to the file take effect.
As far as I can remember I did not change anything important in the
file, it just doesn't work anymore now. Any ideas on what might be going on?
Greetings,
b^4
There was a change in reload code from 0.2 to 0.21. But i don't think
it should change the reload behavior.
It was changed to fix reload problems when the urls are not in the
__main__ module.
Here is the difference between 0.2 and 0.21. Can you try replacing
the __import__ line with the old version.
Index: request.py
===================================================================
--- request.py (revision 149)
+++ request.py (revision 150)
@@ -115,9 +115,7 @@
if not hasattr(inp, '__call__'):
if autoreload:
# black magic to make autoreload work:
- mod = \
- __import__(
- fvars['__file__'].split(os.path.sep).pop().split
('.')[0])
+ mod = __import__(fvars['__name__'], None, None, [""])
#@@probably should replace this with some inspect magic
name = utils.dictfind(fvars, inp)
func = lambda: handle(getattr(mod, name), mod)
Yup, now that I changed it back to the old behaviour, reloading works
fine. Is that enough information or do you need more? Thanks btw, this
is a great fix in the mean time!
b^4
-Justin
What's wrong with using lighttpd? If you set up web.py correctly and
make it use reloader and not cache the templates, there shouldn't be a
difference. It works fine here (with Anand's tip about 0.21).
To the original poster; are you sure you are using web.reloader as is
described in the tutorial on webpy.org?
Greetins,
b^4
Cheers,
Justin
Call web.render with cache=False.
Sorry if I'm talking about things that are none of my business, but I
thought I would mention two things about your code that actually have
nothing to do with the problem:
- PEP 8 [1] recommends using underscores to concatenate words for
variables instead of camel-case (info_object) unless you are using
some library that already uses camel-case and have to follow it.
- The storage class makes your templates look nice :) try this one;
>>> import web
>>> a=web.storify(dict(spam="eggs", tasty="bacon"))
>>> print repr(a)
<Storage {'tasty': 'bacon', 'spam': 'eggs'}>
>>> print a.spam
eggs
Now you can use “a.spam” in your template instead of “a["spam"]”.
About your actual problem; I don't know what could cause that. Does this
also happen when you run web.py in stand-alone mode?
Greetings,
b^4
Comments should be complete sentences. If a comment is a phrase or"
sentence, its first word should be capitalized, unless it is an identifier
that begins with a lower case letter (never alter the case of
identifiers!).
If a comment is short, the period at the end can be omitted. Block
comments generally consist of one or more paragraphs built out of complete
sentences, and each sentence should end in a period.
You should use two spaces after a sentence-ending period.
Rob
PEP: Python Enhancement Proposal. The accepted proposals are
considered standards by most of the python community and it is
generally a good idea to follow them as closely as possible. Once
you'll get used to it you will notice how useful it is when all code
looks the same :) It makes it really easy to understand and read other
code. Note that PEP 8 is just one out of many; there is an entire PEP
just about how docstrings should be written, for example. I would most
certainly recommend having a look at the index ([1]) and reading the
documents relevant to your project.
By "running it in stand-alone mode" I mean doing something like this:
$ python code.py
http://0.0.0.0:8080/
Calling code.py this way makes it run on a built-in webserver that you
can access on port 8080 (as it says in the uri). This is useful for
debugging since you can rule out issues coming from the webserver
configuration.
Good luck and greetings,
b^4
I actually have that old version still and my code.py will not update
if I change it. I have to restart the web server everytime. This is
really not an efficient way to figure things out. I am calling
web.reloader. I don't know what I can do anymore, any have any ideas?
This is my code.py:
#!/usr/bin/env python
import web
import sharewonders
urls = (
'/(.*)', 'colortool.index'
)
class index:
def GET(self):
print "Hello World!"
#nothing
class test:
def GET(self):
print "Test succeeded!"
web.webapi.internalerror = web.debugerror
if __name__ == "__main__": web.run(urls, globals(), web.reloader)
1 - Launch run.py with with a shell script.
#!/bin/sh
while sleep 0; do
echo "RESTARTING (Press ^C twice to exit.)"
./run.py
echo "KILLED"
done
2 - Write the pid to file in run.py.
open('/tmp/webpy.pid', 'w').write(`os.getpid()`)
3 - Bind this shell script to a hotkey.
cat /tmp/webpy.pid | xargs kill
I'm on OS X, and I use Spark.app, which is free. I have to wrap it as
an applescript, which looks like this:
do shell script "cat /tmp/webpy.pid | xargs kill"
This makes development much more pleasant for me. Another thing you
might have it do is switch to your browser after you press the restart
hotkey. (Bind it in the kill script, not launch script.) Let me know
if anyone finds this useful or has any suggestions.
Developer's Site: http://www.shadowlab.org/Software/software.php?sign=Sprk
Download: http://www.shadowlab.org/Software/Spark/Spark.dmg.bin
Development is not supposed to be done using an external webserver but
using webpy's internal webserver. Aside from that; have you already
tried deleting the sockets? These things caused weird caching issues I
could not explain (pages were cached even after restarting lighttpd), so
that might be a good thing to do. /etc/init.d/lighttpd stop && rm -f
/tmp/webpy.sock && /etc/init.d/lighttpd start.
Anyway, good luck.