Skip to content

Commit

Permalink
Link examples with apis (#1264)
Browse files Browse the repository at this point in the history
* updated requirements.txt

* linked examples to api

* fixed bad css for examples. conditionally include examples section

* linking between examples and pi done

* made path relative so that it works with branch prefix

Co-authored-by: Itay Gabbay <itay@deepchecks.com>
  • Loading branch information
shivshankardayal and ItayGabbay committed Apr 14, 2022
1 parent f0aa52e commit 16a9014
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ readthedocs-sphinx-search
numpydoc>=1.1.0
pypandoc>=1.7.2
sphinx-reredirects>=0.0.1
ipywidgets @ git+https://github.com/deepchecks/ipywidgets.git@8ac487b64ffd48dbeaa0a47d1997be27f6052dbc
25 changes: 19 additions & 6 deletions docs/source/_templates/autosummary/check.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@
{# submoduletype = tabular|vision... #}
{% set submoduletype = splitname[1] %}

Example
~~~~~~~
{% set path = getcwd() + '/source/examples/' + submoduletype + '/checks/' + checktype + '/source/plot_' + to_snake_case(objname).lower() + '.py' %}

.. nbgallery::
:name: rst-gallery
:glob:
{%- if path_exists(path) %}
Examples
~~~~~~~~

{{objname}} Example Notebook </examples/{{ submoduletype }}/checks/examples/{{ checktype }}/{{ objname.lower() }}>
.. raw:: html

<div class="sphx-glr-thumbcontainer">

.. only:: html

.. figure:: /examples/{{ submoduletype }}/checks/{{ checktype}}/examples/images/thumb/sphx_glr_plot_{{ to_snake_case(objname).lower() }}_thumb.png
:alt: {{ objname }}

:ref:`sphx_glr_examples_{{submoduletype}}_checks_{{ checktype }}_examples_plot_{{ to_snake_case(objname).lower() }}.py`

.. raw:: html

</div>
<div style="clear:both"></div>
{%- endif %}
{% endblock %}
23 changes: 22 additions & 1 deletion docs/source/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@
});
});
</script>
{% endblock %}
{% endblock %}

{% block docs_main %}
{% if sidebars %}
{% set content_col_class = "col-md-9 col-xl-7" %}
{% else %}
{% set content_col_class = "col-md-11 col-xl-8" %}
{% endif %}
<main class="col-12 {{ content_col_class }} py-md-5 pl-md-5 pr-md-4 bd-content" role="main">
{% if get_check_example_api_reference(pagename) %}
{{ get_check_example_api_reference(pagename) }}
{% endif %}
{% block docs_body %}
<div>
{% block body %} {% endblock %}
</div>
{% endblock %}
{% if theme_show_prev_next %}
{% include "prev-next.html" %}
{% endif %}
</main>
{% endblock %}
46 changes: 40 additions & 6 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,18 @@

# A dictionary of values to pass into the template engine’s context
# for autosummary stubs files.
#
# autosummary_context = {'to_snake_case': to_snake_case}

def path_exists(path: str):
return os.path.exists(path)

def getswd(pth: str):
return os.getcwd()

autosummary_context = {
'to_snake_case': to_snake_case,
'path_exists': path_exists,
'getcwd': os.getcwd
}

# TODO: explaine
autosummary_filename_map = {
Expand Down Expand Up @@ -271,7 +281,6 @@
# Continue copying lines as long as they end with this character
copybutton_line_continuation_character = "\\"


# -- Linkcode ----------------------------------------------------------------

def _import_object_from_name(module_name, fullname):
Expand Down Expand Up @@ -434,6 +443,33 @@ def linkcode_resolve(domain, info):
target = target.strip()
nitpick_ignore.append((dtype, target))

def get_check_example_api_reference(filepath: str) -> t.Optional[str]:
if not (
filepath.startswith("docs/source/examples/tabular/checks/")
or filepath.startswith("docs/source/examples/vision/checks/")
or filepath.startswith("examples/tabular/checks/")
or filepath.startswith("examples/vision/checks/")
):
return ''

notebook_name = snake_case_to_camel_case(
filepath.split("/")[-1][5:]
.replace(".txt", "")
.replace(".ipynb", "")
.replace(".py", "")
)

import deepchecks.checks
check_clazz = getattr(deepchecks.checks, notebook_name, None)

if check_clazz is None or not hasattr(check_clazz, "__module__"):
return

clazz_module = ".".join(check_clazz.__module__.split(".")[:-1])

apipath = f"<ul><li><a href='../../../../../api/generated/{clazz_module}.{notebook_name}.html'>API Reference - {notebook_name}</a></li></ul>"
return apipath

def get_report_issue_url(pagename: str) -> str:
template = (
"https://github.com/{user}/{repo}/issues/new?title={title}&body={body}&labels={labels}"
Expand All @@ -450,14 +486,12 @@ def get_report_issue_url(pagename: str) -> str:
def snake_case_to_camel_case(val: str) -> str:
return "".join(it.capitalize() for it in val.split("_") if it)


# -- Registration of hooks ---------


def setup(app):

def add_custom_routines(app, pagename, templatename, context, doctree):
context["get_report_issue_url"] = get_report_issue_url
context["get_check_example_api_reference"] = get_check_example_api_reference

# make custom routines available within html templates
app.connect("html-page-context", add_custom_routines)
Expand Down

0 comments on commit 16a9014

Please sign in to comment.