Skip to content

Instantly share code, notes, and snippets.

@duarteocarmo
Created July 15, 2019 14:17
Show Gist options
  • Save duarteocarmo/c1d4dd14585981a7955e0904e420ae57 to your computer and use it in GitHub Desktop.
Save duarteocarmo/c1d4dd14585981a7955e0904e420ae57 to your computer and use it in GitHub Desktop.
Automatic report generator
import subprocess
import sys
import papermill as papermill
import os
ONEDRIVE_FOLDER = "shared folder"
LOCAL_FOLDER = "local-copy"
BASE_NOTEBOOK = ".ipynb"
def generate_html_report(notebook_file):
generate = subprocess.run(
[
"jupyter",
"nbconvert",
notebook_file,
"--TagRemovePreprocessor.remove_cell_tags={'hide'}",
"--TagRemovePreprocessor.remove_input_tags={'hide_input'}",
"--to=html",
]
)
print("HTML Report was generated")
return True
def sync_directories():
sync = subprocess.run(
["rclone", "sync", f"remote:{ONEDRIVE_FOLDER}", LOCAL_FOLDER]
)
print("Syncing local directory with cloud....")
return sync.returncode
def run_notebook(excel_report):
no_extension_name = excel_report.split(".")[0]
papermill.execute_notebook(
BASE_NOTEBOOK,
f"{no_extension_name}.ipynb",
parameters=dict(filename=f"{LOCAL_FOLDER + '/' + excel_report}"),
)
return no_extension_name
def get_new_files(remote_folder, local_folder):
cloud_directories = subprocess.run(
["rclone", "lsf", f"remote:{remote_folder}"],
capture_output=True,
text=True,
).stdout.split("\n")[0:-1]
print(f"In the cloud we have: \n{cloud_directories}")
local_directories = subprocess.run(
["ls", local_folder], capture_output=True, text=True
).stdout.split("\n")[0:-1]
print(f"In the local copy we have: \n{local_directories}")
new_files = list(set(cloud_directories) - set(local_directories))
return new_files
def delete_notebook(notebook_name):
os.remove(notebook_name)
return True
def push_to_cloud(remote_folder, html_report):
push = subprocess.run(
["rclone", "copy", html_report, f"remote:{remote_folder}"]
)
print("Report Published!!!")
new_files = get_new_files(
remote_folder=ONEDRIVE_FOLDER, local_folder=LOCAL_FOLDER
)
if not new_files:
print("Everything is synced. No new files.")
sys.exit()
else:
print("There are files missing.")
print(new_files)
sync_directories()
clean_name = run_notebook(new_files[0])
notebook_name = f"{clean_name}.ipynb"
html_report_name = f"{clean_name}.html"
generate_html_report(notebook_name)
push_to_cloud(html_report=html_report_name, remote_folder=ONEDRIVE_FOLDER)
os.remove(notebook_name)
os.remove(html_report_name)
sync_directories()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment