Move environment setting from handler to the notebook application

This commit is contained in:
Cameron Bates 2012-12-06 13:35:28 -08:00
parent 3d08384741
commit 7493794594
2 changed files with 8 additions and 13 deletions

View File

@ -32,8 +32,6 @@ import os
from tornado import web from tornado import web
from tornado import websocket from tornado import websocket
from jinja2 import Environment, FileSystemLoader
from zmq.eventloop import ioloop from zmq.eventloop import ioloop
from zmq.utils import jsonapi from zmq.utils import jsonapi
@ -198,12 +196,6 @@ class AuthenticatedHandler(RequestHandler):
host = self.request.host # get from request host = self.request.host # get from request
return "%s://%s" % (proto, host) return "%s://%s" % (proto, host)
@property
def environment(self):
""" Jinja 2 template base directory
"""
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")))
return env
class AuthenticatedFileHandler(AuthenticatedHandler, web.StaticFileHandler): class AuthenticatedFileHandler(AuthenticatedHandler, web.StaticFileHandler):
"""static files should only be accessible when logged in""" """static files should only be accessible when logged in"""
@ -219,7 +211,7 @@ class ProjectDashboardHandler(AuthenticatedHandler):
def get(self): def get(self):
nbm = self.application.notebook_manager nbm = self.application.notebook_manager
project = nbm.notebook_dir project = nbm.notebook_dir
nb = self.environment.get_template('projectdashboard.html') nb = self.application.jinja2_env.get_template('projectdashboard.html')
self.write( nb.render(project=project, self.write( nb.render(project=project,
base_project_url=self.application.ipython_app.base_project_url, base_project_url=self.application.ipython_app.base_project_url,
base_kernel_url=self.application.ipython_app.base_kernel_url, base_kernel_url=self.application.ipython_app.base_kernel_url,
@ -231,7 +223,7 @@ class ProjectDashboardHandler(AuthenticatedHandler):
class LoginHandler(AuthenticatedHandler): class LoginHandler(AuthenticatedHandler):
def _render(self, message=None): def _render(self, message=None):
nb = self.environment.get_template('login.html') nb = self.application.jinja2_env.get_template('login.html')
self.write( nb.render( self.write( nb.render(
next=self.get_argument('next', default=self.application.ipython_app.base_project_url), next=self.get_argument('next', default=self.application.ipython_app.base_project_url),
read_only=self.read_only, read_only=self.read_only,
@ -268,7 +260,7 @@ class LogoutHandler(AuthenticatedHandler):
else: else:
message = {'warning': 'Cannot log out. Notebook authentication ' message = {'warning': 'Cannot log out. Notebook authentication '
'is disabled.'} 'is disabled.'}
nb = self.environment.get_template('logout.html') nb = self.application.jinja2_env.get_template('logout.html')
self.write( nb.render( self.write( nb.render(
read_only=self.read_only, read_only=self.read_only,
logged_in=self.logged_in, logged_in=self.logged_in,
@ -294,7 +286,7 @@ class NamedNotebookHandler(AuthenticatedHandler):
project = nbm.notebook_dir project = nbm.notebook_dir
if not nbm.notebook_exists(notebook_id): if not nbm.notebook_exists(notebook_id):
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
nb = self.environment.get_template('notebook.html') nb = self.application.jinja2_env.get_template('notebook.html')
self.write( nb.render(project=project, self.write( nb.render(project=project,
notebook_id=notebook_id, notebook_id=notebook_id,
base_project_url=self.application.ipython_app.base_project_url, base_project_url=self.application.ipython_app.base_project_url,
@ -314,7 +306,7 @@ class PrintNotebookHandler(AuthenticatedHandler):
project = nbm.notebook_dir project = nbm.notebook_dir
if not nbm.notebook_exists(notebook_id): if not nbm.notebook_exists(notebook_id):
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id) raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
nb = self.environment.get_template('printnotebook.html') nb = self.application.jinja2_env.get_template('printnotebook.html')
self.write( nb.render( self.write( nb.render(
project=project, project=project,
notebook_id=notebook_id, notebook_id=notebook_id,

View File

@ -33,6 +33,7 @@ import webbrowser
# Third party # Third party
import zmq import zmq
from jinja2 import Environment, FileSystemLoader
# Install the pyzmq ioloop. This has to be done before anything else from # Install the pyzmq ioloop. This has to be done before anything else from
# tornado is imported. # tornado is imported.
@ -186,6 +187,8 @@ class NotebookWebApplication(web.Application):
self.ipython_app = ipython_app self.ipython_app = ipython_app
self.read_only = self.ipython_app.read_only self.read_only = self.ipython_app.read_only
self.log = log self.log = log
self.jinja2_env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")))
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------