2018-02-01 19:13:48 +01:00
|
|
|
import jinja2
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
from operator import itemgetter
|
|
|
|
from collections import namedtuple
|
|
|
|
from os.path import getmtime
|
2018-02-04 16:54:28 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
def extract_metadata(ipynb_file):
|
|
|
|
Metadata = namedtuple('Metadata', ['tags', 'completion_date'])
|
|
|
|
with open(ipynb_file, 'r') as f:
|
|
|
|
j = json.load(f)
|
|
|
|
tags = j['metadata']['tags']
|
|
|
|
completion_date = j['metadata']['completion_date']
|
|
|
|
return Metadata(tags, completion_date)
|
2018-02-01 19:13:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def file_name_to_solution(name):
|
2018-02-04 16:54:28 +01:00
|
|
|
Solution = namedtuple('Solution', ["number", "ipynb", "html", "name", "metadata"])
|
2018-02-01 19:13:48 +01:00
|
|
|
number = int(name.replace("EulerProblem", "").replace(".ipynb", ""))
|
|
|
|
ipynb = name
|
2018-02-04 16:54:28 +01:00
|
|
|
metadata = extract_metadata(ipynb)
|
2018-02-01 19:13:48 +01:00
|
|
|
html = name.replace(".ipynb", ".html")
|
2018-02-04 16:54:28 +01:00
|
|
|
name = name.replace("EulerProblem", "Problem ").replace(".ipynb", "")
|
|
|
|
return Solution(number, ipynb, html, name, metadata)
|
2018-02-01 19:13:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_solution_list(directory="./"):
|
|
|
|
l = [file_name_to_solution(f) for f in os.listdir(directory)
|
|
|
|
if f.endswith(".ipynb") and f.startswith("EulerProblem")]
|
|
|
|
l.sort(key=itemgetter(0))
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
|
|
|
def convert_solutions_to_html(solutions):
|
|
|
|
for s in solutions:
|
|
|
|
if getmtime(s.html) < getmtime(s.ipynb):
|
|
|
|
args = ["ipython", "nbconvert", s.ipynb]
|
|
|
|
subprocess.call(args)
|
2018-02-04 16:54:28 +01:00
|
|
|
|
2018-02-01 19:13:48 +01:00
|
|
|
|
|
|
|
def render_solutions(solutions):
|
|
|
|
loader = jinja2.FileSystemLoader(searchpath="./")
|
|
|
|
env = jinja2.Environment(loader=loader)
|
|
|
|
template = env.get_template("template.html")
|
|
|
|
d = {"solutions": solutions}
|
|
|
|
with open("index.html", 'w') as f:
|
|
|
|
f.write(template.render(**d))
|
|
|
|
|
|
|
|
|
|
|
|
def convert_solutions_to_html(solutions):
|
|
|
|
for s in solutions:
|
2018-02-02 10:05:12 +01:00
|
|
|
if not os.path.isfile(s.html) or getmtime(s.html) < getmtime(s.ipynb):
|
2018-02-01 19:13:48 +01:00
|
|
|
args = ["ipython", "nbconvert", s.ipynb]
|
|
|
|
subprocess.call(args)
|
|
|
|
|
|
|
|
|
|
|
|
def ship_to_failx():
|
|
|
|
for f in os.listdir():
|
|
|
|
if f.endswith(".html"):
|
|
|
|
args = ["scp", f, "failx@felixm.de:~/html/euler/"]
|
|
|
|
subprocess.call(args)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
solutions = get_solution_list()
|
|
|
|
convert_solutions_to_html(solutions)
|
|
|
|
render_solutions(solutions)
|
2018-02-04 16:54:28 +01:00
|
|
|
#ship_to_failx()
|