Skip to content

Instantly share code, notes, and snippets.

@renegarcia
Forked from href/test.py
Last active August 29, 2015 14:19
Show Gist options
  • Save renegarcia/d7e6631d758397547e5f to your computer and use it in GitHub Desktop.
Save renegarcia/d7e6631d758397547e5f to your computer and use it in GitHub Desktop.
import inspect
import morepath
import os
from webob import static
class App(morepath.App):
@property
def static_files(self):
app_path = os.path.dirname(inspect.getfile(self.__class__))
return os.path.join(app_path, 'static')
class StaticFile(object):
def __init__(self, path):
self.path = path
def render_path(content, request):
return request.get_response(static.FileApp(content))
@App.path(model=StaticFile, path='/static', absorb=True)
def get_static_file(app, absorb):
return StaticFile(os.path.join(app.static_files, absorb))
@App.view(model=StaticFile, render=render_path)
def view_static_file(self, request):
return self.path
if __name__ == '__main__':
config = morepath.setup()
config.scan()
config.commit()
if not os.path.exists('./static'):
os.mkdir('./static')
with open('./static/hello.txt', 'w') as f:
f.write('hello world')
morepath.run(App()) # open http://localhost:5000/static/hello.txt after this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment