Skip to content

Commit

Permalink
add sections to flax.linen toctree
Browse files Browse the repository at this point in the history
  • Loading branch information
cgarciae committed May 3, 2023
1 parent bb76e47 commit 73c606b
Show file tree
Hide file tree
Showing 17 changed files with 527 additions and 281 deletions.
76 changes: 76 additions & 0 deletions docs/_ext/flax_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2023 The Flax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Sphinx directive for visualizing Flax modules.
Use directive as follows:
.. flax_module::
:module: flax.linen
:class: Dense
"""

from docutils import nodes
from docutils.parsers.rst import directives
from docutils.statemachine import ViewList

import sphinx
from sphinx.util.docutils import SphinxDirective
from docs.conf_sphinx_patch import generate_autosummary_content
import sphinx.ext.autosummary.generate as ag
import importlib


def render_module(modname: str, qualname: str, app):
parent = importlib.import_module(modname)
obj = getattr(parent, qualname)
template = ag.AutosummaryRenderer(app)
template_name = "flax_module"
imported_members = False
recursive = False
context = {}
return generate_autosummary_content(
qualname, obj, parent, template, template_name, imported_members,
app, recursive, context, modname, qualname)

class FlaxModuleDirective(SphinxDirective):
has_content = True
option_spec = {
'module': directives.unchanged,
'class': directives.unchanged,
}

def run(self):
module_template = render_module(
self.options['module'], self.options['class'], self.env.app
)
module_template = module_template.splitlines()

# Create a container for the rendered nodes
container_node = nodes.container()
self.content = ViewList(module_template, self.content.parent)
self.state.nested_parse(self.content, self.content_offset, container_node)

return [container_node]


def setup(app):
app.add_directive('flax_module', FlaxModuleDirective)

return {
'version': sphinx.__display_version__,
'parallel_read_safe': True,
'parallel_write_safe': True,
}
2 changes: 1 addition & 1 deletion docs/_templates/autosummary/flax_module.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ fullname | escape | underline}}
{{ fullname | escape | underline }}

.. currentmodule:: {{ module }}

Expand Down
278 changes: 0 additions & 278 deletions docs/api_reference/flax.linen.rst

This file was deleted.

Loading

0 comments on commit 73c606b

Please sign in to comment.