Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
improve notebook gallery + switch book is True by default for all doc…
Browse files Browse the repository at this point in the history
…umentation
  • Loading branch information
sdpython committed Dec 24, 2016
1 parent 65a7e58 commit a640662
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
5 changes: 3 additions & 2 deletions _unittests/ut_helpgen/test_notebooks.py
Expand Up @@ -110,13 +110,14 @@ def a_test_notebook(self, iteration):
i, j, str(fou), str(exp))) i, j, str(fou), str(exp)))


file = os.path.join(temp, "all_notebooks.rst") file = os.path.join(temp, "all_notebooks.rst")
add_notebook_page([_[0] for _ in res], file) add_notebook_page([_[0] for _ in res if _[0].endswith(".ipynb")], file)
assert os.path.exists(file) assert os.path.exists(file)


with open(os.path.join(temp, "example_pyquickhelper.rst"), "r", encoding="utf8") as f: with open(os.path.join(temp, "example_pyquickhelper.rst"), "r", encoding="utf8") as f:
text = f.read() text = f.read()
assert "from pyquickhelper.loghelper import fLOG\n fLOG(OutputPrint=False) # by default" in text assert "from pyquickhelper.loghelper import fLOG\n fLOG(OutputPrint=False) # by default" in text
assert ":linenos:" in text if ".. raw:: html" not in text:
raise Exception(text)




if __name__ == "__main__": if __name__ == "__main__":
Expand Down
24 changes: 23 additions & 1 deletion src/pyquickhelper/helpgen/default_conf.py
Expand Up @@ -23,7 +23,7 @@ def set_sphinx_variables(fileconf, module_name, author, year, theme, theme_path,
enable_disabled_parts="enable_disabled_documented_pieces_of_code", enable_disabled_parts="enable_disabled_documented_pieces_of_code",
sharepost="facebook-linkedin-twitter-20-body", custom_style=None, sharepost="facebook-linkedin-twitter-20-body", custom_style=None,
extlinks=None, github_user=None, github_repo=None, title=None, extlinks=None, github_user=None, github_repo=None, title=None,
book=False, link_resolve=None): book=True, link_resolve=None):
""" """
defines variables for Sphinx defines variables for Sphinx
Expand Down Expand Up @@ -630,3 +630,25 @@ def custom_setup(app, author):


# style for notebooks # style for notebooks
app.add_stylesheet(style_figure_notebook[0]) app.add_stylesheet(style_figure_notebook[0])


def get_default_stylesheet():
"""
Returns the style of additional style sheets
@return list of files
.. versionadded:: 1.5
"""
return ["_static/" + style_figure_notebook[0]]


def get_default_javascript():
"""
Returns the style of additional style sheets
@return list of files
.. versionadded:: 1.5
"""
return ["_static/require.js"]
47 changes: 37 additions & 10 deletions src/pyquickhelper/helpgen/process_notebooks.py
Expand Up @@ -659,6 +659,11 @@ def build_thumbail_in_gallery(nbfile, folder_snippet, relative, rst_link):
name += ".thumb" name += ".thumb"
full = os.path.join(folder_snippet, name) full = os.path.join(folder_snippet, name)


dirname = os.path.dirname(full)
if not os.path.exists(dirname):
raise FileNotFoundError("Unable to find folder '{0}'\nfolder_snippet='{1}'\nrelative='{2}'\nnbfile='{3}'".format(
dirname, folder_snippet, relative, nbfile))

if isinstance(image, str): if isinstance(image, str):
# SVG # SVG
full += ".svg" full += ".svg"
Expand Down Expand Up @@ -697,19 +702,25 @@ def add_notebook_page(nbs, fileout):
hier = set() hier = set()
rst = [] rst = []
for tu in nbs: for tu in nbs:
if isinstance(tu, tuple): if isinstance(tu, (tuple, list)):
if tu[0] is None or ("/" not in tu[0] and "\\" not in tu[0]): if tu[0] is None or ("/" not in tu[0] and "\\" not in tu[0]):
rst.append((tuple(), tu[1])) rst.append((tuple(), tu[1]))
else: else:
way = tuple(tu[0].replace("\\", "/").split("/")[:-1]) way = tuple(tu[0].replace("\\", "/").split("/")[:-1])
hier.add(way) hier.add(way)
rst.append((way, tu[1])) rst.append((way, tu[1]))
else: else:
rst.append((tuple(), tu[1])) rst.append((tuple(), tu))
ext = os.path.splitext(rst[-1][1])[-1]
if ext != ".ipynb":
raise ValueError(
"One file is not a notebook: {0}".format(rst[-1][1]))
rst.sort() rst.sort()


folder_index = os.path.dirname(os.path.normpath(fileout)) folder_index = os.path.dirname(os.path.normpath(fileout))
folder = os.path.join(folder_index, "notebooks") folder = os.path.join(folder_index, "notebooks")
if not os.path.exists(folder):
os.mkdir(folder)


if len(hier) == 0: if len(hier) == 0:
rows.append(".. toctree::") rows.append(".. toctree::")
Expand All @@ -729,18 +740,32 @@ def add_notebook_page(nbs, fileout):


link = os.path.splitext(os.path.split(file)[-1])[0] link = os.path.splitext(os.path.split(file)[-1])[0]
link = link.replace("_", "") + "rst" link = link.replace("_", "") + "rst"
rst = build_thumbail_in_gallery(file, folder, folder_index, link) if not os.path.exists(file):
rows.append(rst) raise FileNotFoundError("Unable to find: '{0}'\nRST=\n{1}".format(
file, "\n".join(str(_) for _ in rst)))
r = build_thumbail_in_gallery(file, folder, folder_index, link)
rows.append(r)
else: else:
level = "-+^" level = "-+^"
rows.append("") rows.append("")
rows.append(".. contents::") rows.append(".. contents::")
rows.append(" :depth: 2") rows.append(" :depth: 2")
rows.append("") rows.append("")
stack_file = []
last = None last = None
for hi, r in rst: for hi, r in rst:
rs = os.path.splitext(os.path.split(r)[-1])[0] rs = os.path.splitext(os.path.split(r)[-1])[0]
r0 = r
if hi != last: if hi != last:

for nbf in stack_file:
rs = os.path.splitext(os.path.split(nbf)[-1])[0]
link = rs.replace("_", "") + "rst"
r = build_thumbail_in_gallery(
nbf, folder, folder_index, link)
rows.append(r)
stack_file = []

for k in range(0, len(hi)): for k in range(0, len(hi)):
if last is None or k >= len(last) or hi[k] != last[k]: if last is None or k >= len(last) or hi[k] != last[k]:
break break
Expand All @@ -756,12 +781,14 @@ def add_notebook_page(nbs, fileout):
rows.append("") rows.append("")


rows.append(" notebooks/{0}".format(rs)) rows.append(" notebooks/{0}".format(rs))

stack_file.append(r0)
for hi, r in rst:
rs = os.path.splitext(os.path.split(r)[-1])[0] if len(stack_file) > 0:
link = rs.replace("_", "") + "rst" for nbf in stack_file:
rst = build_thumbail_in_gallery(hi, folder, folder_index, link) rs = os.path.splitext(os.path.split(nbf)[-1])[0]
rows.append(rst) link = rs.replace("_", "") + "rst"
r = build_thumbail_in_gallery(nbf, folder, folder_index, link)
rows.append(r)


rows.append("") rows.append("")
with open(fileout, "w", encoding="utf8") as f: with open(fileout, "w", encoding="utf8") as f:
Expand Down

0 comments on commit a640662

Please sign in to comment.