-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating a script to automatize the conversion of jupyter files to rst
when an example is updated in jupyter notebook files, the script can be used to automatically convert the jupyter files in notebooks folder to rst files saved to source/examples for the documentaiton changes: 1. docs/notebooks_convert.py: the script to read the jupyter files, convert them to rst and save them into example folder 2. docs-requirements.py: updating the requirements with nbformat and nbconvert needed for the conversion
- Loading branch information
1 parent
4a4ecee
commit 361eecf
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from nbconvert import RSTExporter | ||
from nbconvert.writers import FilesWriter | ||
import nbformat | ||
import os | ||
|
||
path = os.path.abspath( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
) | ||
) | ||
def convert_notebook_to_rst(input_file, output_dir,file_name): | ||
# Load the notebook | ||
with open(input_file, 'r', encoding='utf-8') as f: | ||
notebook_content = nbformat.read(f, as_version=4) | ||
|
||
# Create an RST exporter | ||
rst_exporter = RSTExporter() | ||
|
||
# Generate RST content | ||
rst_content, resources = rst_exporter.from_notebook_node(notebook_content) | ||
|
||
if not os.path.exists(output_dir): | ||
os.mkdir(output_dir) | ||
|
||
output_file = f"{output_dir}/{file_name}.rst" | ||
|
||
# Write RST content to the output file | ||
with open(output_file, 'w', encoding='utf-8') as f: | ||
f.write(rst_content) | ||
|
||
print(f"Conversion successful. RST file saved to: {output_file}") | ||
|
||
# Copy images to the output folder | ||
image_dir = output_dir | ||
for image_filename, image_data in resources['outputs'].items(): | ||
image_path = f"{output_dir}/{image_filename}" | ||
with open(image_path, 'wb') as img_file: | ||
img_file.write(image_data) | ||
|
||
def get_all_ipynb_files(examples_path="notebooks"): | ||
|
||
all_files = os.listdir(examples_path) | ||
return [(f"{path}/{examples_path}/{file}",file.split(".ipynb")[0]) for file in all_files if file.endswith("ipynb")] | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
files = get_all_ipynb_files() | ||
|
||
for file,folder in files: | ||
convert_notebook_to_rst(file, f"{path}/source/examples/{folder}",folder) | ||
|