Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jupyter notebook loader and custom template #210

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions klaus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import jinja2
import flask
import httpauth
import dulwich.web
from klaus import views, utils
from klaus.repo import FancyRepo


KLAUS_VERSION = utils.guess_git_revision() or '1.2.2'


Expand All @@ -17,7 +17,12 @@ class Klaus(flask.Flask):

def __init__(self, repo_paths, site_name, use_smarthttp, ctags_policy='none'):
"""(See `make_app` for parameter descriptions.)"""
repo_objs = [FancyRepo(path) for path in repo_paths]

user_dirs = repo_paths
repo_objs = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes seem unrelated

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! These changes are for my self usage. Didn't notice these commits will be related to this pull request. I've checkout a new branch and made a new pull request. It was my first time doing pull request, don't know if it's the right way 😃

Copy link
Author

@lzfxxx lzfxxx Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#211 new request, I've moved some of your comments to there

for user_dir in user_dirs:
user_repo_paths = [os.path.join(user_dir, x) for x in os.listdir(user_dir)]
repo_objs += [FancyRepo(path) for path in user_repo_paths]
self.repos = dict((repo.name, repo) for repo in repo_objs)
self.site_name = site_name
self.use_smarthttp = use_smarthttp
Expand Down Expand Up @@ -48,24 +53,24 @@ def create_jinja_environment(self):

def setup_routes(self):
for endpoint, rule in [
('repo_list', '/'),
('robots_txt', '/robots.txt/'),
('blob', '/<repo>/blob/'),
('blob', '/<repo>/blob/<rev>/<path:path>'),
('blame', '/<repo>/blame/'),
('blame', '/<repo>/blame/<rev>/<path:path>'),
('raw', '/<repo>/raw/<path:path>/'),
('raw', '/<repo>/raw/<rev>/<path:path>'),
('submodule', '/<repo>/submodule/<rev>/'),
('submodule', '/<repo>/submodule/<rev>/<path:path>'),
('commit', '/<repo>/commit/<path:rev>/'),
('patch', '/<repo>/commit/<path:rev>.diff'),
('patch', '/<repo>/commit/<path:rev>.patch'),
('index', '/<repo>/'),
('index', '/<repo>/<path:rev>'),
('history', '/<repo>/tree/<rev>/'),
('history', '/<repo>/tree/<rev>/<path:path>'),
('download', '/<repo>/tarball/<path:rev>/'),
('repo_list', '/'),
('robots_txt', '/robots.txt/'),
('blob', '/<repo>/blob/'),
('blob', '/<repo>/blob/<rev>/<path:path>'),
('blame', '/<repo>/blame/'),
('blame', '/<repo>/blame/<rev>/<path:path>'),
('raw', '/<repo>/raw/<path:path>/'),
('raw', '/<repo>/raw/<rev>/<path:path>'),
('submodule', '/<repo>/submodule/<rev>/'),
('submodule', '/<repo>/submodule/<rev>/<path:path>'),
('commit', '/<repo>/commit/<path:rev>/'),
('patch', '/<repo>/commit/<path:rev>.diff'),
('patch', '/<repo>/commit/<path:rev>.patch'),
('index', '/<repo>/'),
('index', '/<repo>/<path:rev>'),
('history', '/<repo>/tree/<rev>/'),
('history', '/<repo>/tree/<rev>/<path:path>'),
('download', '/<repo>/tarball/<path:rev>/'),
]:
self.add_url_rule(rule, view_func=getattr(views, endpoint))

Expand All @@ -80,7 +85,6 @@ def should_use_ctags(self, git_repo, git_commit):
raise ValueError("Unknown ctags policy %r" % self.ctags_policy)



def make_app(repo_paths, site_name, use_smarthttp=False, htdigest_file=None,
require_browser_auth=False, disable_push=False, unauthenticated_push=False,
ctags_policy='none'):
Expand Down Expand Up @@ -128,7 +132,7 @@ def make_app(repo_paths, site_name, use_smarthttp=False, htdigest_file=None,
if use_smarthttp:
# `path -> Repo` mapping for Dulwich's web support
dulwich_backend = dulwich.server.DictBackend(
dict(('/'+name, repo) for name, repo in app.repos.items())
dict(('/' + name, repo) for name, repo in app.repos.items())
)
# Dulwich takes care of all Git related requests/URLs
# and passes through everything else to klaus
Expand Down
17 changes: 16 additions & 1 deletion klaus/markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,20 @@ def render_rest(content):
LANGUAGES.append((['.rst', '.rest'], render_rest))


for loader in [_load_markdown, _load_restructured_text]:
def _load_notebook():
try:
import nbformat
import nbconvert
except ImportError:
return

def render_notebook(content):
nb = nbformat.reads(content, nbformat.NO_CONVERT)
(output, resources) = nbconvert.HTMLExporter(template_file='./klaus/templates/my_full.tpl').from_notebook_node(nb)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my_full? Confusing name :-D

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a little CSS modification of nbconvert template 'full', cause the width in @media setting of that template will make the html overflow the markup div. The naming is not very clear, do you have any suggestions?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! In this case could we simply use the default template and add the CSS fixes to klaus' CSS file? Or if that doesn't work, can we use inheritance to override the CSS instead of copy-pasting the whole file?

return output

LANGUAGES.append((['.ipynb'], render_notebook))


for loader in [_load_markdown, _load_restructured_text, _load_notebook]:
loader()
4 changes: 3 additions & 1 deletion klaus/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def name(self):
2. /x/y/ -> /x/y
3. /x/y -> y
"""
return self.path.replace(".git", "").rstrip(os.sep).split(os.sep)[-1]
path_arr = self.path.replace(".git", "").rstrip(os.sep).split(os.sep)
# join user_ID and repo name by '\', to distinguish repo belong to different user
return path_arr[-2] + '\\' + path_arr[-1]

def get_last_updated_at(self):
"""Get datetime of last commit to this repository."""
Expand Down
98 changes: 98 additions & 0 deletions klaus/templates/my_full.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{%- extends 'basic.tpl' -%}
{% from 'mathjax.tpl' import mathjax %}


{%- block header -%}
<!DOCTYPE html>
<html>
<head>
{%- block html_head -%}
<meta charset="utf-8" />
<title>{{resources['metadata']['name']}}</title>

{%- if "widgets" in nb.metadata -%}
<script src="https://unpkg.com/[email protected].*/dist/embed.js"></script>
{%- endif-%}

<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

{% for css in resources.inlining.css -%}
<style type="text/css">
{{ css }}
</style>
{% endfor %}

<style type="text/css">
/* Overrides of notebook CSS for static HTML export */
body {
overflow: visible;
padding: 8px;
}

div#notebook {
overflow: visible;
border-top: none;
}

{%- if resources.global_content_filter.no_prompt-%}
div#notebook-container{
padding: 6ex 12ex 8ex 12ex;
}
{%- endif -%}

@media print {
div.cell {
display: block;
page-break-inside: avoid;
}
div.output_wrapper {
display: block;
page-break-inside: avoid;
}
div.output {
display: block;
page-break-inside: avoid;
}
}

@media (min-width: 768px) {
.container {
width: 728px;
}
}
@media (min-width: 992px) {
.container {
width: 900px;
}
}
@media (min-width: 1200px) {
.container {
width: 1100px;
}
}
</style>

<!-- Custom stylesheet, it must be in the same directory as the html file -->
<link rel="stylesheet" href="custom.css">

<!-- Loading mathjax macro -->
{{ mathjax() }}
{%- endblock html_head -%}
</head>
{%- endblock header -%}

{% block body %}
<body>
<div tabindex="-1" id="notebook" class="border-box-sizing">
<div class="container" id="notebook-container">
{{ super() }}
</div>
</div>
</body>
{%- endblock body %}

{% block footer %}
{{ super() }}
</html>
{% endblock footer %}
6 changes: 3 additions & 3 deletions klaus/templates/skeleton.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<script src={{ url_for('static', filename='klaus.js') }}></script>

<header>
<a href={{ url_for('repo_list') }}>
{{ SITE_NAME }}
</a>
{# <a href={{ url_for('repo_list') }}>#}
{# {{ SITE_NAME }}#}
{# </a>#}
<span class=breadcrumbs>{% block breadcrumbs %}{% endblock %}</span>
{% block extra_header %}{% endblock %}
</header>
Expand Down
5 changes: 5 additions & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
klaus
pytest
requests
markdown
docutils
nbformat
nbconvert
python-ctags3>=1.2.1
mock; python_version < '3'