From d8112a62966e10789822c022bdfea2a945439c4c Mon Sep 17 00:00:00 2001
From: firley <konrad.firley@student.reutlingen-university.de>
Date: Sat, 20 Apr 2024 16:10:03 +0200
Subject: [PATCH] added conversion script that converts all ipynb file to json
 and it work cascading -> converts every thing in a folder even in sub and
 subsub... folders

---
 convert.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 convert.py

diff --git a/convert.py b/convert.py
new file mode 100644
index 0000000..4076e88
--- /dev/null
+++ b/convert.py
@@ -0,0 +1,46 @@
+import json
+import os 
+from os import listdir
+#jupyter nbconvert --to html */*/*.ipynb <- command converts every ipynb to html
+
+############################################################################################################
+# Info: this scipt is used to create converted copys of ipynb notebooks.
+#       The Copy is a converted json files. And they shoul be put automagically in the same folder. 
+#       It works as intended, only the console output might be a little confusing because 
+#       it is still a work in Progress.
+#
+# Running: put in all missing bits, like your file path and then it is able to run 
+# Filepath: should be the top folder where all files are at -> machine-learning-services-main
+#
+# Any Questions? ask Konrad :) 
+#############################################################################################################
+
+#like this: /./Users/Lomikukus/Coding/ML_Projekt/machine-learning-services-main/
+
+directory = os.fsencode('/./Users/Lomikukus/Coding/ML_Projekt/machine-learning-services-main/') 
+
+f = []
+
+#walks over all files and folders that are listed in the folder which u used in path
+for (root, dirnames, filenames) in os.walk(directory): #this outputs a list of all roots, directorie names, and filenames 
+    
+    for file in filenames:  #for each file found...
+        #sprint(file)
+        pathtofile = os.path.join(root, file)
+        #print(pathtofile)
+        filename = os.fsdecode(file)
+        filenameX = str(filename)
+        #print(filenameX)
+        if filename.endswith(".ipynb"): #...it checks if the file ends in .ipynb...
+            print(os.path.join(root, file))
+            print("found")
+
+            with open(pathtofile, mode="r", encoding="utf-8") as sf: #...if true then they are being corectly converted to a good json format 
+                mynotebook = json.loads(sf.read())
+                namejson = "notebook.json"
+                namejson = os.fsencode(namejson)
+                newfilepath = os.path.join(root, namejson)
+                with open(newfilepath, mode="w", encoding="utf-8") as newjson:
+                    json.dump(mynotebook, newjson, ensure_ascii=False, indent=4)
+                    print("converted")
+
-- 
GitLab