File size: 3,127 Bytes
2c3bda2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695c309
775c9ff
 
2c3bda2
 
695c309
d77157b
775c9ff
d77157b
 
775c9ff
d77157b
775c9ff
 
2c3bda2
775c9ff
 
2c3bda2
d77157b
2c3bda2
d77157b
2c3bda2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d77157b
2c3bda2
d77157b
2c3bda2
 
 
 
 
 
 
d77157b
2c3bda2
d77157b
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# import streamlit as st
# import tempfile
# import subprocess
# import os

# st.set_page_config(page_title="Colab Notebook β†’ PDF", page_icon="πŸ“˜")

# st.title("πŸ“˜ Colab Notebook β†’ PDF Converter (LaTeX-based)")

# st.write("Upload your `.ipynb` notebook below to convert it to a PDF using LaTeX (nbconvert).")

# uploaded_file = st.file_uploader("Upload your notebook", type=["ipynb"])

# if uploaded_file is not None:
#     with tempfile.NamedTemporaryFile(delete=False, suffix=".ipynb") as tmp:
#         tmp.write(uploaded_file.read())
#         tmp_path = tmp.name

#     st.info("🧩 Converting notebook to PDF... Please wait (1–2 min).")

#     try:
#         # Run nbconvert (LaTeX-based PDF export)
#         subprocess.run(
#             ["jupyter", "nbconvert", "--to", "pdf", tmp_path],
#             check=True,
#             stdout=subprocess.PIPE,
#             stderr=subprocess.PIPE,
#         )

#         pdf_file = tmp_path.replace(".ipynb", ".pdf")

#         with open(pdf_file, "rb") as f:
#             st.download_button(
#                 label="⬇️ Download PDF",
#                 data=f,
#                 file_name="converted_notebook.pdf",
#                 mime="application/pdf",
#             )

#         st.success("βœ… Conversion successful!")

#     except subprocess.CalledProcessError as e:
#         st.error("❌ Conversion failed.")
#         st.exception(e)
import streamlit as st
import tempfile
import os
from nbconvert import PDFExporter
from traitlets.config import Config

st.set_page_config(page_title="Colab Notebook β†’ PDF", page_icon="πŸ“˜")

st.title("πŸ“˜ Colab Notebook β†’ PDF Converter (LaTeX-based)")
st.write("Upload your `.ipynb` notebook below to convert it to a PDF using LaTeX (nbconvert).")

uploaded_file = st.file_uploader("Upload your notebook", type=["ipynb"])

if uploaded_file is not None:
    with tempfile.NamedTemporaryFile(delete=False, suffix=".ipynb", mode='wb') as tmp:
        tmp.write(uploaded_file.read())
        tmp_path = tmp.name
    
    st.info("🧩 Converting notebook to PDF... Please wait (1–2 min).")
    
    try:
        # Configure PDF exporter
        c = Config()
        c.PDFExporter.latex_command = ['xelatex', '{filename}']
        
        # Create PDF exporter and convert
        pdf_exporter = PDFExporter(config=c)
        pdf_data, resources = pdf_exporter.from_filename(tmp_path)
        
        # Save PDF temporarily
        pdf_path = tmp_path.replace(".ipynb", ".pdf")
        with open(pdf_path, "wb") as f:
            f.write(pdf_data)
        
        # Offer download
        st.download_button(
            label="⬇️ Download PDF",
            data=pdf_data,
            file_name="converted_notebook.pdf",
            mime="application/pdf",
        )
        
        st.success("βœ… Conversion successful!")
        
        # Cleanup
        os.unlink(tmp_path)
        if os.path.exists(pdf_path):
            os.unlink(pdf_path)
            
    except Exception as e:
        st.error("❌ Conversion failed.")
        st.error(f"Error details: {str(e)}")
        st.exception(e)