From 442fdcbbab6e890b91d961bf4174ad4623cc2e0b Mon Sep 17 00:00:00 2001 From: Mohammad Amin Tahavori <50955527+mohammadamint@users.noreply.github.com> Date: Fri, 1 Dec 2023 23:19:29 +0100 Subject: [PATCH] lint notebooks_converter.py script --- docs/notebooks_convert.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/notebooks_convert.py b/docs/notebooks_convert.py index 42cd60b4..c2295294 100644 --- a/docs/notebooks_convert.py +++ b/docs/notebooks_convert.py @@ -3,14 +3,12 @@ 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): +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: + with open(input_file, "r", encoding="utf-8") as f: notebook_content = nbformat.read(f, as_version=4) # Create an RST exporter @@ -25,28 +23,32 @@ def convert_notebook_to_rst(input_file, output_dir,file_name): 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: + 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(): + for image_filename, image_data in resources["outputs"].items(): image_path = f"{output_dir}/{image_filename}" - with open(image_path, 'wb') as img_file: + 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")] + 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) - + for file, folder in files: + convert_notebook_to_rst(file, f"{path}/source/examples/{folder}", folder)