-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '3d0623678923e98594ad2d6e443372e71efd6e6d'
- Loading branch information
Showing
4 changed files
with
257 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset='utf-8' /> | ||
<title>nbviewer.js</title> | ||
|
||
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/themes/prism.min.css'/> | ||
<script src='https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.6/marked.min.js'></script> | ||
<script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/prism.min.js' data-manual></script> | ||
<script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/components/prism-python.min.js' data-manual></script> | ||
|
||
<style type='text/css'> | ||
body { | ||
font: 0.8em Arial, sans-serif; | ||
background-color: #eee; | ||
} | ||
div#instructions { | ||
max-width: 960px; | ||
font-size: 2em; | ||
color: #aaa; | ||
text-align: center; | ||
/*padding-top: 5%;*/ | ||
margin: 0 auto; | ||
} | ||
p#examples { | ||
font-size: .5em; | ||
} | ||
div#serving-info { | ||
font-size: .7em; | ||
font-style: italic; | ||
margin-top: 1em; | ||
} | ||
div#bugs { | ||
font-size: .6em; | ||
} | ||
input#ghproj { | ||
display: block; | ||
font-size: 1em; | ||
text-align: center; | ||
margin: 0 auto; | ||
width: 50%; | ||
} | ||
div#filelist { | ||
margin: 0 auto; | ||
width: 50%; | ||
text-align: left; | ||
font-size: .8em; | ||
} | ||
div#filelist ul { | ||
list-style-type: none; | ||
} | ||
div#filelist ul li { | ||
vertical-align: center; | ||
} | ||
div#filelist ul li a { | ||
text-decoration: none; | ||
} | ||
</style> | ||
|
||
<!-- nbviewer.js --> | ||
<script src='lib/nbv.js'></script> | ||
</head> | ||
<body> | ||
<a href="https://github.com/kokes/nbviewer.js"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a> | ||
|
||
<div id='doc'> | ||
<div id='instructions'> | ||
<p>Enter a <code>username/repo</code> combination from Github</p> | ||
<p id='examples'> | ||
<a href='#aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9yc3ZwL2ZlY29uMjM1L2NvbnRlbnRzL25iL2ZyZWQtZW1wbG95LW5mcC5pcHluYj9yZWY9bWFzdGVy'>Example 1</a> | ||
<a href='#aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9hcm9rZW0vdGVhY2hfb3B0aW1pemF0aW9uL2NvbnRlbnRzL29wdGltaXphdGlvbi5pcHluYj9yZWY9bWFzdGVy'>Example 2</a> | ||
<form> | ||
<input id='ghproj' type='text' placeholder='username/repo' /> | ||
<input id='ghlookup' type='submit' value='Browse repo' /> | ||
</form> | ||
|
||
<div id='serving-info'></div> | ||
<div id='bugs'>(We know there's an encoding issue making non-latin characters render incorrectly.)</div> | ||
|
||
<div id='filelist'> | ||
|
||
</div> | ||
</div> | ||
<div id='notebook'> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
|
||
<script type='text/javascript'> | ||
var d = document; | ||
var cache = {}; | ||
var gg; // debugging | ||
|
||
window.onload = function() { | ||
var lk = d.getElementById('ghlookup'); | ||
var ghproj = d.getElementById('ghproj'); | ||
|
||
// block submitting again | ||
lk.addEventListener('click', function(e) { | ||
e.target.disabled = 'disabled'; | ||
// TODO: validate inputs | ||
// should be something like [a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-], not sure what exactly | ||
var turl = get_gh_url('repos/' + ghproj.value + '/contents/'); // living on the edgeeee | ||
window.location.hash = btoa(turl); | ||
}); | ||
// re-enable the submit button when changed | ||
ghproj.addEventListener('keyup', function(e) { | ||
lk.disabled = ''; | ||
}); | ||
hash_changed(); | ||
}; | ||
|
||
window.onhashchange = hash_changed; | ||
|
||
function hash_changed() { | ||
render_gh_files(); | ||
} | ||
|
||
function render_gh_files() { | ||
wipeElement(d.getElementById('serving-info')); // clear | ||
var hs = window.location.hash.slice(1); // without '#' | ||
var hsurl = atob(hs); // base64 -> text | ||
ajax_get_json(hsurl, function(dt) { | ||
gg = dt; | ||
// get pathname | ||
var a = d.createElement('a'); | ||
a.href = hsurl; | ||
var path = a.pathname; | ||
|
||
if (path.endsWith('ipynb')) { | ||
var nb = JSON.parse(atob(dt.content)); // decode incoming data | ||
var tg = d.getElementById('notebook'); | ||
nbv.render(nb, tg); | ||
|
||
// serving info | ||
var turl = window.location; | ||
d.getElementById('serving-info').innerHTML = `Serving <code>${dt.name}</code>. <a href='${turl}'>Link to this render</a> / <a href='${dt.html_url}'>original Github source</a>`; | ||
return; | ||
} | ||
|
||
var fl = d.getElementById('filelist'); | ||
var ul = d.createElement('ul'); | ||
wipeElement(fl); | ||
for (var j=0; j<dt.length; j++) { | ||
var el = dt[j]; | ||
var li = d.createElement('li'); | ||
li.appendChild(function() { | ||
if (el.type === 'file' && !el.name.endsWith('.ipynb')) { | ||
return d.createTextNode(el.name); | ||
} | ||
|
||
var a = d.createElement('a'); | ||
a.textContent = el.name; | ||
a.setAttribute('href', '#' + btoa(el.url)); | ||
return a; | ||
}()); | ||
|
||
ul.appendChild(li); | ||
} | ||
fl.appendChild(ul); | ||
|
||
}); | ||
} | ||
|
||
function get_gh_url(path) { | ||
return 'https://api.github.com/' + path; | ||
} | ||
|
||
function ajax_get_json(url, callback) | ||
{ | ||
console.log(url); | ||
if (cache[url] !== undefined) { | ||
console.log('serving from cache'); | ||
return callback(cache[url]); | ||
} | ||
|
||
var rr = new XMLHttpRequest(); | ||
rr.onreadystatechange = function() { | ||
if (rr.readyState == 4 && rr.status == 200) { | ||
cache[url] = JSON.parse(rr.responseText) | ||
callback(JSON.parse(rr.responseText)); | ||
} else if (rr.readyState == 4) { | ||
throw Error(err); | ||
} | ||
} | ||
rr.open('GET', url); | ||
rr.send(null); | ||
} | ||
|
||
function wipeElement(el) { | ||
while (el.firstChild) { | ||
el.removeChild(el.firstChild); | ||
} | ||
} | ||
|
||
</script> | ||
|
||
<script> | ||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | ||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | ||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | ||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | ||
|
||
ga('create', 'UA-98368126-1', 'auto'); | ||
ga('send', 'pageview'); | ||
|
||
</script> | ||
|
||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters