Spaces:
Running
Running
download
Browse files
app.py
CHANGED
|
@@ -3,34 +3,61 @@ from datasets import load_dataset
|
|
| 3 |
import nbformat
|
| 4 |
from nbconvert import HTMLExporter
|
| 5 |
from traitlets.config import Config
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
config = Config()
|
| 9 |
config.HTMLExporter.preprocessors = ["nbconvert.preprocessors.ExtractOutputPreprocessor"]
|
| 10 |
-
|
| 11 |
-
# Instantiate the exporter. We use the `classic` template for now; we'll get into more details
|
| 12 |
-
# later about how to customize the exporter further.
|
| 13 |
html_exporter = HTMLExporter(config=config, template_name="classic")
|
| 14 |
|
| 15 |
ds = load_dataset("data-agents/kaggle-notebooks", split="train", streaming=True)
|
| 16 |
ds_iter = iter(ds)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def parse_notebook():
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
notebook_parsed = nbformat.reads(notebook_string, as_version=4)
|
| 21 |
(notebook_body, resources) = html_exporter.from_notebook_node(notebook_parsed)
|
|
|
|
| 22 |
print("Resources:", resources["outputs"])
|
| 23 |
return notebook_body
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
gr.Markdown("# Kaggle Notebooks")
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
with gr.Row():
|
| 31 |
with gr.Column():
|
| 32 |
html = gr.HTML("")
|
| 33 |
-
|
| 34 |
button.click(fn=parse_notebook, inputs=[], outputs=[html])
|
|
|
|
| 35 |
demo.load(fn=parse_notebook, inputs=[], outputs=[html])
|
|
|
|
| 36 |
demo.launch()
|
|
|
|
| 3 |
import nbformat
|
| 4 |
from nbconvert import HTMLExporter
|
| 5 |
from traitlets.config import Config
|
| 6 |
+
import os
|
| 7 |
+
import shutil
|
| 8 |
|
| 9 |
+
# Configuration for HTMLExporter
|
| 10 |
config = Config()
|
| 11 |
config.HTMLExporter.preprocessors = ["nbconvert.preprocessors.ExtractOutputPreprocessor"]
|
|
|
|
|
|
|
|
|
|
| 12 |
html_exporter = HTMLExporter(config=config, template_name="classic")
|
| 13 |
|
| 14 |
ds = load_dataset("data-agents/kaggle-notebooks", split="train", streaming=True)
|
| 15 |
ds_iter = iter(ds)
|
| 16 |
|
| 17 |
+
TMP_DIR = './tmp/'
|
| 18 |
+
|
| 19 |
+
def reset_tmp_folder():
|
| 20 |
+
if os.path.exists(TMP_DIR):
|
| 21 |
+
shutil.rmtree(TMP_DIR)
|
| 22 |
+
os.makedirs(TMP_DIR)
|
| 23 |
+
|
| 24 |
def parse_notebook():
|
| 25 |
+
reset_tmp_folder()
|
| 26 |
+
|
| 27 |
+
notebook_data = next(ds_iter)
|
| 28 |
+
notebook_string = notebook_data["text"]
|
| 29 |
+
notebook_id = notebook_data["id"].split("/")[-1]
|
| 30 |
+
|
| 31 |
+
# Save the notebook string to a file
|
| 32 |
+
with open(os.path.join(TMP_DIR, notebook_id), 'w') as f:
|
| 33 |
+
f.write(notebook_string)
|
| 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
|
| 40 |
|
| 41 |
+
def download_notebook():
|
| 42 |
+
for filename in os.listdir(TMP_DIR):
|
| 43 |
+
if filename.endswith('.ipynb'):
|
| 44 |
+
path = os.path.join(TMP_DIR, filename)
|
| 45 |
+
print(path)
|
| 46 |
+
return path
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("# Kaggle Notebooks")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
button = gr.Button("Show next!")
|
| 53 |
+
button_download = gr.Button("Download notebook")
|
| 54 |
+
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column():
|
| 57 |
html = gr.HTML("")
|
| 58 |
+
|
| 59 |
button.click(fn=parse_notebook, inputs=[], outputs=[html])
|
| 60 |
+
button_download.click(fn=download_notebook, inputs=[], outputs=[])
|
| 61 |
demo.load(fn=parse_notebook, inputs=[], outputs=[html])
|
| 62 |
+
|
| 63 |
demo.launch()
|