colab2pdf / src /streamlit_app.py
rishav000's picture
Update src/streamlit_app.py
2c3bda2 verified
# 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)