I had to fix a few things to get this to run without error on Jupyter Notebook on Windows 10. Not sure exactly why the final output does not look exactly like the the website version. The CSS file is probably not being picked up correctly.
Don't have privileges to do pull request on this repo yet, so I cannot merge into main branch.
`#!/usr/bin/env python3
""" Here's the same code but not executed inside a main() function."""
I had to make a few modifications to get it to render the templated ouptut HTML on Windows 10 correctly.
import datetime
import sys
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup
import pystache
url = 'http://docs.python.org/2/library/datetime.html'
body = urllib.request.urlopen(url).read()
soup = BeautifulSoup(body, features="html.parser")
table = soup.find(id='strftime-and-strptime-behavior').find('table')
example_date = datetime.datetime(2013, 9, 30, 7, 6, 5)
directives = []
for row in table.select('tbody > tr'):
tds = row.find_all('td')
directive = tds[0].find('span').string
# we use getText() here because some of the meanings have extra markup
meaning = tds[1].getText().replace('\n', ' ')
example = example_date.strftime(directive)
directives.append({
'directive': directive,
'meaning': meaning,
'example': example
})
# Also add the non zero padded version for some of them
# http://stackoverflow.com/questions/28894172/why-does-d-or-e-remove-the-leading-space-or-zero
exclude = ['%f', '%y']
if 'zero-padded' in meaning and directive not in exclude:
non_zero_padding_decimal = directive.replace("%", "%#")
non_zero_padding_example = example_date.strftime(
non_zero_padding_decimal
)
non_zero_padding_meaning = (
meaning.replace("zero-padded", "") + " (Platform specific)")
directives.append({
'directive': non_zero_padding_decimal,
'meaning': non_zero_padding_meaning,
'example': non_zero_padding_example
})
template_file = '.\\strftime.org-master\\templates\\index.html.mustache'
template = open(template_file).read()
context = {
'example_date': str(example_date),
'example_date_repr': repr(example_date),
'directives': directives,
'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%d'),
}
# Render the final context into the HTML template file
rendered_html = pystache.render(template, context)
# create / open the output file for writing.
output_html = open("rendered_html.html", "wt")
# Now write out the output file.
output_html.write(rendered_html)
output_html.close()
print(pystache.render(template, context))
I had to fix a few things to get this to run without error on Jupyter Notebook on Windows 10. Not sure exactly why the final output does not look exactly like the the website version. The CSS file is probably not being picked up correctly.
Don't have privileges to do pull request on this repo yet, so I cannot merge into main branch.
`#!/usr/bin/env python3
""" Here's the same code but not executed inside a main() function."""
Taken from the reference page's github module "build.py" located at https://github.com/mccutchen/strftime.org"""
I had to make a few modifications to get it to render the templated ouptut HTML on Windows 10 correctly.