Spaces:
Running
Running
more refactor
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from nbconvert import HTMLExporter
|
|
| 5 |
from traitlets.config import Config
|
| 6 |
import os
|
| 7 |
import shutil
|
|
|
|
| 8 |
|
| 9 |
# Configuration for HTMLExporter
|
| 10 |
config = Config()
|
|
@@ -21,11 +22,29 @@ def reset_tmp_folder():
|
|
| 21 |
shutil.rmtree(TMP_DIR)
|
| 22 |
os.makedirs(TMP_DIR)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def parse_notebook():
|
| 25 |
reset_tmp_folder()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
out_path = os.path.join(TMP_DIR, notebook_id)
|
| 30 |
|
| 31 |
# Save the notebook string to a file
|
|
@@ -34,25 +53,17 @@ def parse_notebook():
|
|
| 34 |
|
| 35 |
notebook_parsed = nbformat.reads(notebook_string, as_version=4)
|
| 36 |
(notebook_body, resources) = html_exporter.from_notebook_node(notebook_parsed)
|
| 37 |
-
|
| 38 |
-
print("Resources:", resources["outputs"])
|
| 39 |
return notebook_body, out_path
|
| 40 |
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
gr.Markdown("# Kaggle Notebooks")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#button_download = gr.Button("Download notebook")
|
| 48 |
-
|
| 49 |
-
with gr.Row():
|
| 50 |
-
with gr.Column():
|
| 51 |
-
html = gr.HTML("")
|
| 52 |
|
| 53 |
-
with gr.Row():
|
| 54 |
-
with gr.Column():
|
| 55 |
-
file = gr.File()
|
| 56 |
|
| 57 |
button.click(fn=parse_notebook, inputs=[], outputs=[html, file])
|
| 58 |
demo.load(fn=parse_notebook, inputs=[], outputs=[html, file])
|
|
|
|
| 5 |
from traitlets.config import Config
|
| 6 |
import os
|
| 7 |
import shutil
|
| 8 |
+
import base64
|
| 9 |
|
| 10 |
# Configuration for HTMLExporter
|
| 11 |
config = Config()
|
|
|
|
| 22 |
shutil.rmtree(TMP_DIR)
|
| 23 |
os.makedirs(TMP_DIR)
|
| 24 |
|
| 25 |
+
|
| 26 |
+
def embed_figures(html_body, resources):
|
| 27 |
+
for key, value in resources['outputs'].items():
|
| 28 |
+
b64_figure = base64.b64encode(value).decode('utf-8')
|
| 29 |
+
img_tag = f'data:image/png;base64,{b64_figure}'
|
| 30 |
+
html_body = html_body.replace(key, img_tag)
|
| 31 |
+
return html_body
|
| 32 |
+
|
| 33 |
def parse_notebook():
|
| 34 |
reset_tmp_folder()
|
| 35 |
+
|
| 36 |
+
found_notebook = False
|
| 37 |
+
counter = 0
|
| 38 |
+
while not found_notebook:
|
| 39 |
+
counter+=1
|
| 40 |
+
print(counter)
|
| 41 |
+
notebook_data = next(ds_iter)
|
| 42 |
+
notebook_string = notebook_data["text"]
|
| 43 |
+
notebook_id = notebook_data["id"].split("/")[-1]
|
| 44 |
+
|
| 45 |
+
if len(notebook_string)>1 * 1024 * 1024:
|
| 46 |
+
found_notebook = True
|
| 47 |
+
|
| 48 |
out_path = os.path.join(TMP_DIR, notebook_id)
|
| 49 |
|
| 50 |
# Save the notebook string to a file
|
|
|
|
| 53 |
|
| 54 |
notebook_parsed = nbformat.reads(notebook_string, as_version=4)
|
| 55 |
(notebook_body, resources) = html_exporter.from_notebook_node(notebook_parsed)
|
| 56 |
+
notebook_body = embed_figures(notebook_body, resources)
|
| 57 |
+
print("Resources:", resources["outputs"].keys())
|
| 58 |
return notebook_body, out_path
|
| 59 |
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("# Kaggle Notebooks")
|
| 63 |
+
button = gr.Button("Show next!")
|
| 64 |
+
file = gr.File()
|
| 65 |
+
html = gr.HTML("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
button.click(fn=parse_notebook, inputs=[], outputs=[html, file])
|
| 69 |
demo.load(fn=parse_notebook, inputs=[], outputs=[html, file])
|