Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate examples in documentation (#603) #625

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pyproject.toml
.idea/
.DS_Store
schema/*/build/
workflows/
3 changes: 3 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def _parse_release_as_version(rls):
# ones.
extensions = [
'sphinx.ext.todo'
'sphinx.ext.autodoc', # For pulling in docstrings
'sphinx.ext.doctest', # For testing code examples in documentation
'sphinx.ext.napoleon', # For Google and NumPy-style docstrings
]

# Add any paths that contain templates here, relative to this directory.
Expand Down
8 changes: 8 additions & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Examples
========

.. code-block:: python

.. code-block:: python

>>> multiply(2, 3)
10 changes: 10 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def multiply(a, b):
"""
Multiply two numbers together.

Example:
>>> multiply(2, 3)
6
"""
return a * b
45 changes: 45 additions & 0 deletions scripts/update_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os

def extract_examples(file_path):
"""Extract examples from source files."""
examples = []
with open(file_path, 'r') as f:
for line in f:
if line.strip().startswith(">>>"):
examples.append(line.strip())
return examples

def update_docs(doc_path, examples):
"""Insert updated examples into documentation."""
print(f"Reading from: {doc_path}") # Debugging output
with open(doc_path, 'r') as f:
content = f.readlines()

updated_content = []
inside_placeholder = False
for line in content:
if line.strip() == "# Examples will be dynamically updated here.":
inside_placeholder = True
updated_content.append(".. code-block:: python\n\n")
for example in examples:
updated_content.append(f" {example}\n")
elif inside_placeholder and line.strip() == "":
inside_placeholder = False
elif not inside_placeholder:
updated_content.append(line)

with open(doc_path, 'w') as f:
f.writelines(updated_content)
print(f"Updated {doc_path} with new examples.") # Debugging output

# Example usage
source_file_path = 'examples/example.py' # Path to the source code file
doc_file_path = 'docs/source/examples.rst' # Path to the documentation file
Comment on lines +36 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I believe we would like to use our validation models that are defined here as the source and update the example section that corresponds to that model in docs/source/concepts.

For example, CopyNumberCount validation model would generate the docs example here. We'd want the docs example to also add the id field which is the out.ga4gh_identify field in the validation model.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification! I will use the validation models as the source for generating the documentation examples. specifically I will extract the relevant data from each model, including the out.ga4gh_identify field and update the example section in docs/source/concepts. I’ll proceed with implementing this and updating the documentation accordingly.


print(f"Extracting examples from: {source_file_path}") # Debugging output
examples = extract_examples(source_file_path) # Pass the actual file path here
print(f"Extracted examples: {examples}") # Debugging output

print(f"Updating documentation: {doc_file_path}") # Debugging output
update_docs(doc_file_path, examples)
print("Documentation updated successfully!") # Final success message