naohiro701 commited on
Commit
040f78f
·
verified ·
1 Parent(s): 7265a81

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +13 -22
src/streamlit_app.py CHANGED
@@ -19,34 +19,25 @@ def compile_latex(source: str) -> bytes:
19
  bytes
20
  The compiled PDF file contents.
21
  """
22
- # Create a sandbox directory for all generated files
23
  with tempfile.TemporaryDirectory() as tmpdir:
24
- # Initialize document with an arbitrary base name
25
- doc = Document(
26
- # This sets the base filename for output (without extension)
27
- "document",
28
- documentclass="article",
29
- font_size="10pt",
30
- )
31
  doc.append(source)
32
 
33
- # Full path without extension
34
- output_base = os.path.join(tmpdir, "document")
35
- # Generate PDF; this writes 'document.pdf' into tmpdir
36
  _ = doc.generate_pdf(
37
- filepath=output_base,
38
- clean=True, # remove aux files like .aux, .log
39
- clean_tex=True, # also remove the .tex file
40
- silent=True # suppress compiler output
 
41
  )
42
 
43
- # Read the compiled PDF back into memory
44
- pdf_path = Path(output_base + ".pdf")
45
- return pdf_path.read_bytes()
46
 
47
  def main():
48
- """Streamlit entry point."""
49
- st.title("LaTeX Compiler on Hugging Face Space")
50
  prompt = st.text_area(
51
  "Enter full LaTeX source",
52
  value=(
@@ -60,8 +51,8 @@ def main():
60
 
61
  if st.button("Compile to PDF"):
62
  with st.spinner("Compiling…"):
63
- pdf_bytes = compile_latex(source=prompt)
64
- st.success("Done")
65
  st.download_button(
66
  "Download PDF",
67
  data=pdf_bytes,
 
19
  bytes
20
  The compiled PDF file contents.
21
  """
 
22
  with tempfile.TemporaryDirectory() as tmpdir:
23
+ # ドキュメントを初期化
24
+ doc = Document("document", documentclass="article", font_size="10pt")
 
 
 
 
 
25
  doc.append(source)
26
 
27
+ base_path = os.path.join(tmpdir, "document")
28
+ # compiler 引数で pdflatex を指定。latexmk があれば省略可。
 
29
  _ = doc.generate_pdf(
30
+ filepath=base_path,
31
+ compiler="pdflatex",
32
+ clean=True,
33
+ clean_tex=True,
34
+ silent=True,
35
  )
36
 
37
+ return Path(base_path + ".pdf").read_bytes()
 
 
38
 
39
  def main():
40
+ st.title("LaTeX Compiler")
 
41
  prompt = st.text_area(
42
  "Enter full LaTeX source",
43
  value=(
 
51
 
52
  if st.button("Compile to PDF"):
53
  with st.spinner("Compiling…"):
54
+ pdf_bytes = compile_latex(prompt)
55
+ st.success("Compilation succeeded")
56
  st.download_button(
57
  "Download PDF",
58
  data=pdf_bytes,