rishav000 commited on
Commit
2c3bda2
Β·
verified Β·
1 Parent(s): d77157b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +77 -24
src/streamlit_app.py CHANGED
@@ -1,44 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import tempfile
3
- import subprocess
4
  import os
 
 
5
 
6
  st.set_page_config(page_title="Colab Notebook β†’ PDF", page_icon="πŸ“˜")
7
 
8
  st.title("πŸ“˜ Colab Notebook β†’ PDF Converter (LaTeX-based)")
9
-
10
  st.write("Upload your `.ipynb` notebook below to convert it to a PDF using LaTeX (nbconvert).")
11
 
12
  uploaded_file = st.file_uploader("Upload your notebook", type=["ipynb"])
13
 
14
  if uploaded_file is not None:
15
- with tempfile.NamedTemporaryFile(delete=False, suffix=".ipynb") as tmp:
16
  tmp.write(uploaded_file.read())
17
  tmp_path = tmp.name
18
-
19
  st.info("🧩 Converting notebook to PDF... Please wait (1–2 min).")
20
-
21
  try:
22
- # Run nbconvert (LaTeX-based PDF export)
23
- subprocess.run(
24
- ["jupyter", "nbconvert", "--to", "pdf", tmp_path],
25
- check=True,
26
- stdout=subprocess.PIPE,
27
- stderr=subprocess.PIPE,
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  )
29
-
30
- pdf_file = tmp_path.replace(".ipynb", ".pdf")
31
-
32
- with open(pdf_file, "rb") as f:
33
- st.download_button(
34
- label="⬇️ Download PDF",
35
- data=f,
36
- file_name="converted_notebook.pdf",
37
- mime="application/pdf",
38
- )
39
-
40
  st.success("βœ… Conversion successful!")
41
-
42
- except subprocess.CalledProcessError as e:
 
 
 
 
 
43
  st.error("❌ Conversion failed.")
 
44
  st.exception(e)
 
1
+ # import streamlit as st
2
+ # import tempfile
3
+ # import subprocess
4
+ # import os
5
+
6
+ # st.set_page_config(page_title="Colab Notebook β†’ PDF", page_icon="πŸ“˜")
7
+
8
+ # st.title("πŸ“˜ Colab Notebook β†’ PDF Converter (LaTeX-based)")
9
+
10
+ # st.write("Upload your `.ipynb` notebook below to convert it to a PDF using LaTeX (nbconvert).")
11
+
12
+ # uploaded_file = st.file_uploader("Upload your notebook", type=["ipynb"])
13
+
14
+ # if uploaded_file is not None:
15
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".ipynb") as tmp:
16
+ # tmp.write(uploaded_file.read())
17
+ # tmp_path = tmp.name
18
+
19
+ # st.info("🧩 Converting notebook to PDF... Please wait (1–2 min).")
20
+
21
+ # try:
22
+ # # Run nbconvert (LaTeX-based PDF export)
23
+ # subprocess.run(
24
+ # ["jupyter", "nbconvert", "--to", "pdf", tmp_path],
25
+ # check=True,
26
+ # stdout=subprocess.PIPE,
27
+ # stderr=subprocess.PIPE,
28
+ # )
29
+
30
+ # pdf_file = tmp_path.replace(".ipynb", ".pdf")
31
+
32
+ # with open(pdf_file, "rb") as f:
33
+ # st.download_button(
34
+ # label="⬇️ Download PDF",
35
+ # data=f,
36
+ # file_name="converted_notebook.pdf",
37
+ # mime="application/pdf",
38
+ # )
39
+
40
+ # st.success("βœ… Conversion successful!")
41
+
42
+ # except subprocess.CalledProcessError as e:
43
+ # st.error("❌ Conversion failed.")
44
+ # st.exception(e)
45
  import streamlit as st
46
  import tempfile
 
47
  import os
48
+ from nbconvert import PDFExporter
49
+ from traitlets.config import Config
50
 
51
  st.set_page_config(page_title="Colab Notebook β†’ PDF", page_icon="πŸ“˜")
52
 
53
  st.title("πŸ“˜ Colab Notebook β†’ PDF Converter (LaTeX-based)")
 
54
  st.write("Upload your `.ipynb` notebook below to convert it to a PDF using LaTeX (nbconvert).")
55
 
56
  uploaded_file = st.file_uploader("Upload your notebook", type=["ipynb"])
57
 
58
  if uploaded_file is not None:
59
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".ipynb", mode='wb') as tmp:
60
  tmp.write(uploaded_file.read())
61
  tmp_path = tmp.name
62
+
63
  st.info("🧩 Converting notebook to PDF... Please wait (1–2 min).")
64
+
65
  try:
66
+ # Configure PDF exporter
67
+ c = Config()
68
+ c.PDFExporter.latex_command = ['xelatex', '{filename}']
69
+
70
+ # Create PDF exporter and convert
71
+ pdf_exporter = PDFExporter(config=c)
72
+ pdf_data, resources = pdf_exporter.from_filename(tmp_path)
73
+
74
+ # Save PDF temporarily
75
+ pdf_path = tmp_path.replace(".ipynb", ".pdf")
76
+ with open(pdf_path, "wb") as f:
77
+ f.write(pdf_data)
78
+
79
+ # Offer download
80
+ st.download_button(
81
+ label="⬇️ Download PDF",
82
+ data=pdf_data,
83
+ file_name="converted_notebook.pdf",
84
+ mime="application/pdf",
85
  )
86
+
 
 
 
 
 
 
 
 
 
 
87
  st.success("βœ… Conversion successful!")
88
+
89
+ # Cleanup
90
+ os.unlink(tmp_path)
91
+ if os.path.exists(pdf_path):
92
+ os.unlink(pdf_path)
93
+
94
+ except Exception as e:
95
  st.error("❌ Conversion failed.")
96
+ st.error(f"Error details: {str(e)}")
97
  st.exception(e)