Skip to content

Commit

Permalink
Merge pull request #2363 from crbates/jinja2
Browse files Browse the repository at this point in the history
Refactor notebook templates to use Jinja2
  • Loading branch information
Carreau committed Dec 14, 2012
2 parents 3f09b76 + f9bb934 commit 5669d32
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 57 deletions.
46 changes: 22 additions & 24 deletions IPython/frontend/html/notebook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import threading
import time
import uuid
import os

from tornado import web
from tornado import websocket
Expand Down Expand Up @@ -194,7 +195,7 @@ def ws_url(self):
if host == '':
host = self.request.host # get from request
return "%s://%s" % (proto, host)


class AuthenticatedFileHandler(AuthenticatedHandler, web.StaticFileHandler):
"""static files should only be accessible when logged in"""
Expand All @@ -209,28 +210,28 @@ class ProjectDashboardHandler(AuthenticatedHandler):
@authenticate_unless_readonly
def get(self):
nbm = self.application.notebook_manager
project = nbm.notebook_dir
self.render(
'projectdashboard.html', project=project,
project = nbm.notebook_dir
template = self.application.jinja2_env.get_template('projectdashboard.html')
self.write( template.render(project=project,
base_project_url=self.application.ipython_app.base_project_url,
base_kernel_url=self.application.ipython_app.base_kernel_url,
read_only=self.read_only,
logged_in=self.logged_in,
login_available=self.login_available
)
login_available=self.login_available))


class LoginHandler(AuthenticatedHandler):

def _render(self, message=None):
self.render('login.html',
def _render(self, message=None):
template = self.application.jinja2_env.get_template('login.html')
self.write( template.render(
next=self.get_argument('next', default=self.application.ipython_app.base_project_url),
read_only=self.read_only,
logged_in=self.logged_in,
login_available=self.login_available,
base_project_url=self.application.ipython_app.base_project_url,
message=message
)
))

def get(self):
if self.current_user:
Expand Down Expand Up @@ -259,13 +260,13 @@ def get(self):
else:
message = {'warning': 'Cannot log out. Notebook authentication '
'is disabled.'}

self.render('logout.html',
template = self.application.jinja2_env.get_template('logout.html')
self.write( template.render(
read_only=self.read_only,
logged_in=self.logged_in,
login_available=self.login_available,
base_project_url=self.application.ipython_app.base_project_url,
message=message)
message=message))


class NewHandler(AuthenticatedHandler):
Expand All @@ -277,27 +278,24 @@ def get(self):
notebook_id = nbm.new_notebook()
self.redirect('/'+urljoin(self.application.ipython_app.base_project_url, notebook_id))


class NamedNotebookHandler(AuthenticatedHandler):

@authenticate_unless_readonly
def get(self, notebook_id):
nbm = self.application.notebook_manager
project = nbm.notebook_dir
if not nbm.notebook_exists(notebook_id):
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)

self.render(
'notebook.html', project=project,
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
template = self.application.jinja2_env.get_template('notebook.html')
self.write( template.render(project=project,
notebook_id=notebook_id,
base_project_url=self.application.ipython_app.base_project_url,
base_kernel_url=self.application.ipython_app.base_kernel_url,
kill_kernel=False,
read_only=self.read_only,
logged_in=self.logged_in,
login_available=self.login_available,
mathjax_url=self.application.ipython_app.mathjax_url,
)
mathjax_url=self.application.ipython_app.mathjax_url,))


class PrintNotebookHandler(AuthenticatedHandler):
Expand All @@ -307,10 +305,10 @@ def get(self, notebook_id):
nbm = self.application.notebook_manager
project = nbm.notebook_dir
if not nbm.notebook_exists(notebook_id):
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)

self.render(
'printnotebook.html', project=project,
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
template = self.application.jinja2_env.get_template('printnotebook.html')
self.write( template.render(
project=project,
notebook_id=notebook_id,
base_project_url=self.application.ipython_app.base_project_url,
base_kernel_url=self.application.ipython_app.base_kernel_url,
Expand All @@ -319,7 +317,7 @@ def get(self, notebook_id):
logged_in=self.logged_in,
login_available=self.login_available,
mathjax_url=self.application.ipython_app.mathjax_url,
)
))

#-----------------------------------------------------------------------------
# Kernel handlers
Expand Down
3 changes: 3 additions & 0 deletions IPython/frontend/html/notebook/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

# Third party
import zmq
from jinja2 import Environment, FileSystemLoader

# Install the pyzmq ioloop. This has to be done before anything else from
# tornado is imported.
Expand Down Expand Up @@ -186,6 +187,8 @@ def __init__(self, ipython_app, kernel_manager, notebook_manager,
self.ipython_app = ipython_app
self.read_only = self.ipython_app.read_only
self.log = log
self.jinja2_env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), "templates")))



#-----------------------------------------------------------------------------
Expand Down
19 changes: 10 additions & 9 deletions IPython/frontend/html/notebook/templates/notebook.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% extends page.html %}
{% extends "page.html" %}

{% block stylesheet %}

{% if mathjax_url %}
<script type="text/javascript" src="{{mathjax_url}}?config=TeX-AMS_HTML-full&delayStartupUntil=configured" charset="utf-8"></script>
{% end %}
{% endif %}
<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
Expand All @@ -20,9 +21,7 @@
<link rel="stylesheet" href="{{ static_url("css/renderedhtml.css") }}" type="text/css" />

<link rel="stylesheet" href="{{ static_url("css/printnotebook.css") }}" type="text/css" media="print"/>

{% end %}

{% endblock %}

{% block params %}

Expand All @@ -32,7 +31,7 @@
data-read-only={{read_only and not logged_in}}
data-notebook-id={{notebook_id}}

{% end %}
{% endblock %}


{% block header %}
Expand All @@ -42,7 +41,7 @@
<span id="save_status"></span>
</span>

{% end %}
{% endblock %}


{% block site %}
Expand Down Expand Up @@ -177,7 +176,7 @@
<div id='tooltip' class='ipython_tooltip ui-corner-all' style='display:none'></div>


{% end %}
{% endblock %}


{% block script %}
Expand Down Expand Up @@ -222,5 +221,7 @@
<script src="{{ static_url("js/notebookmain.js") }}" type="text/javascript" charset="utf-8"></script>

<script src="{{ static_url("js/contexthint.js") }}" charset="utf-8"></script>
{% endblock %}



{% end %}
23 changes: 14 additions & 9 deletions IPython/frontend/html/notebook/templates/page.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@


<!DOCTYPE HTML>
{% macro static_url(name) -%}
/static/{{ name }}
{%- endmacro %}
<html>

<head>
<meta charset="utf-8">

<title>{% block title %}IPython Notebook{% end %}</title>
<title>{% block title %}IPython Notebook{% endblock %}</title>

<link rel="stylesheet" href="{{static_url("jquery/css/themes/base/jquery-ui.min.css") }}" type="text/css" />
<link rel="stylesheet" href="{{static_url("css/boilerplate.css") }}" type="text/css" />
<link rel="stylesheet" href="{{static_url("css/fbm.css") }}" type="text/css" />
<link rel="stylesheet" href="{{static_url("css/page.css") }}" type="text/css"/>
{% block stylesheet %}
{% end %}
{% endblock %}
<link rel="stylesheet" href="{{ static_url("css/custom.css") }}" type="text/css" />


{% block meta %}
{% end %}
{% endblock %}

</head>

<body {% block params %}{% end %}>
<body {% block params %}{% endblock %}>

<div id="header">
<span id="ipython_notebook"><div><a href={{base_project_url}} alt='dashboard'><img src='{{static_url("ipynblogo.png") }}' alt='IPython Notebook'/></a></div></span>
Expand All @@ -32,18 +37,18 @@
<button id="logout">Logout</button>
{% elif login_available and not logged_in %}
<button id="login">Login</button>
{% end %}
{% endif %}
</span>

{% end %}
{% endblock %}

{% block header %}
{% end %}
{% endblock %}
</div>

<div id="site">
{% block site %}
{% end %}
{% endblock %}
</div>

<script src="{{static_url("jquery/js/jquery-1.7.1.min.js") }}" type="text/javascript" charset="utf-8"></script>
Expand All @@ -53,7 +58,7 @@
<script src="{{static_url("js/loginwidget.js") }}" type="text/javascript" charset="utf-8"></script>

{% block script %}
{% end %}
{% endblock %}

<script src="{{static_url("js/custom.js") }}" type="text/javascript" charset="utf-8"></script>

Expand Down
14 changes: 7 additions & 7 deletions IPython/frontend/html/notebook/templates/printnotebook.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% extends page.html %}
{% extends "page.html" %}

{% block stylesheet %}

{% if mathjax_url %}
<script type="text/javascript" src="{{mathjax_url}}?config=TeX-AMS_HTML-full&delayStartupUntil=configured" charset="utf-8"></script>
{% end %}
{% endif %}
<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
Expand All @@ -20,7 +20,7 @@
<link rel="stylesheet" href="{{ static_url("css/printnotebook.css") }}" type="text/css" />
<link rel="stylesheet" href="{{ static_url("css/renderedhtml.css") }}" type="text/css" />

{% end %}
{% endblock %}


{% block params %}
Expand All @@ -31,11 +31,11 @@
data-read-only={{read_only and not logged_in}}
data-notebook-id={{notebook_id}}

{% end %}
{% endblock %}


{% block header %}
{% end %}
{% endblock %}


{% block site %}
Expand All @@ -48,7 +48,7 @@

</div>

{% end %}
{% endblock %}


{% block script %}
Expand Down Expand Up @@ -78,4 +78,4 @@
<script src="{{ static_url("js/notebook.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{ static_url("js/printnotebookmain.js") }}" type="text/javascript" charset="utf-8"></script>

{% end %}
{% endblock %}
16 changes: 8 additions & 8 deletions IPython/frontend/html/notebook/templates/projectdashboard.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% extends page.html %}
{% extends "page.html" %}

{% block title %}IPython Dashboard{% end %}
{% block title %}IPython Dashboard{% endblock %}

{% block stylesheet %}
<link rel="stylesheet" href="{{static_url("css/projectdashboard.css") }}" type="text/css" />
<link rel="stylesheet" href="{{static_url("css/alternateuploadform.css") }}" type="text/css" />
{% end %}
<link rel="stylesheet" href="{{static_url("css/alternateuploadform.css") }}" type="text/css" />
{% endblock %}


{% block params %}
Expand All @@ -15,7 +15,7 @@
data-base-kernel-url={{base_kernel_url}}
data-read-only={{read_only}}

{% end %}
{% endblock %}


{% block site %}
Expand All @@ -42,7 +42,7 @@
<button id="new_notebook" title="Create new notebook">New Notebook</button>
</span>
</div>
{% end %}
{% endif %}

<div id="notebook_list">
<div id="project_name"><h2>{{project}}</h2></div>
Expand Down Expand Up @@ -72,10 +72,10 @@

</div>

{% end %}
{% endblock %}

{% block script %}
<script src="{{static_url("js/notebooklist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("js/clusterlist.js") }}" type="text/javascript" charset="utf-8"></script>
<script src="{{static_url("js/projectdashboardmain.js") }}" type="text/javascript" charset="utf-8"></script>
{% end %}
{% endblock %}

0 comments on commit 5669d32

Please sign in to comment.