Skip to content

Instantly share code, notes, and snippets.

@mtayseer
Last active August 29, 2015 13:57
Show Gist options
  • Save mtayseer/9561227 to your computer and use it in GitHub Desktop.
Save mtayseer/9561227 to your computer and use it in GitHub Desktop.
Launch a webserver to view generated output & at the same time watch the content for changes & rebuild it.
# [Pelican](http://docs.getpelican.com/) is a static site generator. I'm using it for building [my own website](http://mtayseer.net/).
# To make the editing experience easier, I created this script to launch a webserver to view generated output & at the same time watch
# the content for changes & rebuild it.
#
# I'm using [Bottle](http://bottlepy.org/) for the webserver & [watchdog](https://pythonhosted.org/watchdog/) to watch directory
#
from bottle import route, run, static_file
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
def watch_n_serve():
from bottle import route, run, static_file
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
OUTPUT_ROOT = './output'
@route('/')
@route('/<path:path>')
def serve(path='index.html'):
response = static_file(path, root=OUTPUT_ROOT)
# if the user wants a directory, I search for index pages
if path.endswith('/') and response.status_code == 404:
for file in ['index.html', 'index.htm']:
response = static_file(path + file, root=OUTPUT_ROOT)
if response.status_code != 404:
return response
return response
class BuildEventHandler(FileSystemEventHandler):
def on_moved(self, event):
super(BuildEventHandler, self).on_moved(event)
rebuild()
on_created = on_deleted = on_modified = on_moved
event_handler = BuildEventHandler()
observer = Observer()
observer.schedule(event_handler, './content', recursive=True)
observer.start()
try:
run(host='localhost', port=8080, debug=True)
finally:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment