Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example server using livereload 2.0, Flask, and formic to monitor filesystem changes, re-build files, and run a simple static web server
#!/usr/bin/env python
#
# simple static Flask fileserving app
# with livereload integration
#
from flask import Flask
STATIC_FOLDER = "."
app = Flask(__name__,
static_folder=STATIC_FOLDER,
static_url_path="")
@app.route("/")
def root():
return app.send_static_file("index.html")
#
# livereload server
#
from livereload import Server, shell
from formic import FileSet
from os import getcwd, path
def make_livereload_server(wsgi_app):
server = Server(wsgi_app)
watch_patterns = (
"/src/**",
"/static/**"
)
build_cmd = "make"
print "Files being monitored:"
cwd = getcwd()
for pattern in watch_patterns:
print "Pattern: ", pattern
for filepath in FileSet(include=pattern):
print "=>", path.relpath(filepath, cwd)
server.watch(filepath, build_cmd)
print
return server
import logging
def main():
# wire livereload to Flask via wsgi
flask_wsgi_app = app.wsgi_app
server = make_livereload_server(flask_wsgi_app)
# serve application
server.serve()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment