File size: 1,690 Bytes
f4465d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import gradio as gr

def convert_notebook_to_markdown(file):
    """
    Convert a Jupyter notebook file to Markdown format.
    
    Args:
        file: Uploaded file object from Gradio
    Returns:
        str: Converted markdown content
    """
    try:
        # Read the notebook content
        content = json.load(file)
        
        # Initialize markdown output
        markdown_output = []
        
        # Process each cell
        for cell in content['cells']:
            # Handle markdown cells
            if cell['cell_type'] == 'markdown':
                markdown_output.extend(cell['source'])
                markdown_output.append('\n')
                
            # Handle code cells
            elif cell['cell_type'] == 'code':
                markdown_output.append('```python\n')
                markdown_output.extend(cell['source'])
                markdown_output.append('\n```\n')
        
        # Join all content
        final_markdown = ''.join(markdown_output)
        
        return final_markdown
        
    except Exception as e:
        return f"Error converting notebook: {str(e)}"

# Create Gradio interface
iface = gr.Interface(
    fn=convert_notebook_to_markdown,
    inputs=gr.File(
        label="Upload Jupyter Notebook (.ipynb)",
        file_types=[".ipynb"]
    ),
    outputs=gr.Textbox(
        label="Converted Markdown",
        lines=20
    ),
    title="Jupyter Notebook to Markdown Converter",
    description="Upload a Jupyter notebook (.ipynb) file to convert it to Markdown format. Code cells will be wrapped in Python code blocks.",
    examples=[],
    cache_examples=False
)

# Launch the app
iface.launch()