2

I have pserve --reload working with any python changes, but I still have to manually reload when I change a template.

I made this little script that monitors my templates folder for any changes, but what is the command to reload pserve? If I need to call a pserve method from within my Pyramids project, like in init.py or something, what is the method I would call to reload pserve?

#!/usr/bin/env python
import sys
import pyinotify
from subprocess import call
import time

wm = pyinotify.WatchManager()
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_MODIFY(self, event):
        print "Modified: ", event.pathname

        # This is where my reload call would go...            
        # call(["pserve", "reload"])

handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch("/path/to/my/pyramid/templates/", mask, rec=True, auto_add=True)

notifier.loop()
the4tress
  • 542
  • 6
  • 19
  • add code showing how you setup you jinja2 pyramid environment into pyramid Configurator object (your_project.__init__.py and a sample *.ini PasteDeploy file) – Sascha Gottfried May 24 '14 at 11:33

3 Answers3

6

Pyramid already provides a method to reload templates without restarting pserve by putting such configuration in a PasteDeploy development.ini file or using environment variables. See Environment Variables and .ini File Settings. Of course, do not enable template reloading in production as it slows down your application.

Sascha Gottfried
  • 3,303
  • 20
  • 30
Steve Piercy
  • 13,693
  • 1
  • 44
  • 57
  • Weird. I have pyramid.reload_templates set to true, but when I make changes to my jinja2 templates pserve doesn't reload. Looks like it should according to this: http://docs.pylonsproject.org/projects/pyramid-jinja2/en/latest/ – the4tress May 23 '14 at 21:13
  • pserve won't reload the entire process when a template is changed; it's only necessary to do this when a Python file is changed. However, if pyramid.reload_templates is true, the changes you make to the file will be reflected immediately. – Chris McDonough May 24 '14 at 16:33
  • 1
    So this didn't actually do the trick in my work environment, but I installed pyramid and jinja2 on a machine at home and it worked. I'll look into the problem more at work. Thanks for the help. – the4tress May 28 '14 at 00:24
1

By the way - nice workaround script. Steve is right, pyramid offers that out-of-the-box. But pyramid built-in watchdog behaviour depends also on your configuration. Including pyramid_jinja2 into your pyramid project triggers a lot of stuff in pyramid and makes some assumptions about template file extensions (*.jinja2) as well.

pyramid_jinja offers configuration for template reloading - it can be enabled/disabled as well. Since pyramid 1.5 (do you use it?) you can use setting `pyramid.reload_templates' as well.

But the best thing you can do is to try the pyramid scaffold that uses jinja2 templates. Takes you 5 minutes to setup, then you change a jinja2 template and see if pyramid is reloading or not. If pyramid is reloading, what I expect, you can continue to debug the problem with your current application.

Sascha Gottfried
  • 3,303
  • 20
  • 30
  • Excellent input. Thank you very much. It gives me something to dig into deeper. I installed pyramid_jinja2, but never ran the setup.py for it. I just updated my development.ini and MINIFEST.in files so I could use the jinja2 templates. I will run the setup.py on Monday and compare the differences. On Monday I will mark one of these as "answered". – the4tress May 25 '14 at 01:38
1

Make sure you set your config object to take in the settings from the development.ini as well. Something like config = Configurator(settings = settings)

ForFunAndProfit
  • 1,138
  • 13
  • 17