Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def convert_notebook_to_markdown(file):
|
5 |
+
"""
|
6 |
+
Convert a Jupyter notebook file to Markdown format.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
file: Uploaded file object from Gradio
|
10 |
+
Returns:
|
11 |
+
str: Converted markdown content
|
12 |
+
"""
|
13 |
+
try:
|
14 |
+
# Read the notebook content
|
15 |
+
content = json.load(file)
|
16 |
+
|
17 |
+
# Initialize markdown output
|
18 |
+
markdown_output = []
|
19 |
+
|
20 |
+
# Process each cell
|
21 |
+
for cell in content['cells']:
|
22 |
+
# Handle markdown cells
|
23 |
+
if cell['cell_type'] == 'markdown':
|
24 |
+
markdown_output.extend(cell['source'])
|
25 |
+
markdown_output.append('\n')
|
26 |
+
|
27 |
+
# Handle code cells
|
28 |
+
elif cell['cell_type'] == 'code':
|
29 |
+
markdown_output.append('```python\n')
|
30 |
+
markdown_output.extend(cell['source'])
|
31 |
+
markdown_output.append('\n```\n')
|
32 |
+
|
33 |
+
# Join all content
|
34 |
+
final_markdown = ''.join(markdown_output)
|
35 |
+
|
36 |
+
return final_markdown
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error converting notebook: {str(e)}"
|
40 |
+
|
41 |
+
# Create Gradio interface
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=convert_notebook_to_markdown,
|
44 |
+
inputs=gr.File(
|
45 |
+
label="Upload Jupyter Notebook (.ipynb)",
|
46 |
+
file_types=[".ipynb"]
|
47 |
+
),
|
48 |
+
outputs=gr.Textbox(
|
49 |
+
label="Converted Markdown",
|
50 |
+
lines=20
|
51 |
+
),
|
52 |
+
title="Jupyter Notebook to Markdown Converter",
|
53 |
+
description="Upload a Jupyter notebook (.ipynb) file to convert it to Markdown format. Code cells will be wrapped in Python code blocks.",
|
54 |
+
examples=[],
|
55 |
+
cache_examples=False
|
56 |
+
)
|
57 |
+
|
58 |
+
# Launch the app
|
59 |
+
iface.launch()
|