tomo2chin2 commited on
Commit
6db3d20
·
verified ·
1 Parent(s): 7fd1134

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -47
app.py CHANGED
@@ -1,9 +1,4 @@
1
  import gradio as gr
2
- import weasyprint
3
- import base64
4
- import os
5
- from bs4 import BeautifulSoup # HTML解析用
6
-
7
 
8
  def display_html(html_code):
9
  return f"""
@@ -89,37 +84,6 @@ a:hover {
89
  }
90
  """
91
 
92
- def generate_pdf_and_link(html_code):
93
- """
94
- HTMLからPDFを生成し、プレビューとダウンロードリンクを返す。
95
- """
96
- # 1. HTMLプレビューの生成 (display_htmlを再利用)
97
- preview_html = display_html(html_code)
98
-
99
- # 2. PDFファイル名の決定
100
- soup = BeautifulSoup(html_code, 'html.parser')
101
- title_tag = soup.find('title')
102
- if title_tag:
103
- file_name = title_tag.string.strip() + ".pdf" # 空白文字を除去
104
- else:
105
- file_name = "output.pdf"
106
-
107
- # 3. PDF生成
108
- html = weasyprint.HTML(string=html_code)
109
- pdf_bytes = html.write_pdf()
110
-
111
- # 4. PDFを一時ファイルとして保存 (Hugging Face Spaceでは/tmpが書き込み可能)
112
- pdf_path = os.path.join('/tmp', file_name)
113
- with open(pdf_path, 'wb') as f:
114
- f.write(pdf_bytes)
115
-
116
- # 5. ダウンロードリンクの作成
117
- # GradioのFileコンポーネントを使ってダウンロードリンクを作る
118
- download_link = pdf_path
119
-
120
- # 6. プレビューとリンクを返す
121
- return preview_html, download_link
122
-
123
  with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
124
  gr.Markdown(
125
  """
@@ -134,14 +98,11 @@ with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
134
  placeholder="ここにHTMLコードを貼り付けてください...",
135
  lines=10
136
  )
137
- with gr.Row():
138
- submit_button = gr.Button("表示", variant="secondary") # スタイルを統一
139
- pdf_button = gr.Button("PDFで保存", variant="secondary")
140
 
141
  with gr.Column(scale=3):
142
  output_html = gr.HTML(label="プレビュー")
143
- output_pdf = gr.File(label="PDFダウンロード") # ファイル出力コンポーネント
144
-
145
 
146
  submit_button.click(
147
  display_html,
@@ -149,12 +110,6 @@ with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
149
  outputs=output_html
150
  )
151
 
152
- pdf_button.click(
153
- generate_pdf_and_link,
154
- inputs=input_textbox,
155
- outputs=[output_html, output_pdf] # プレビューとファイルを出力
156
- )
157
-
158
  #サンプル
159
  gr.Examples(
160
  [["<h1>Hello world</h1>"],["""
 
1
  import gradio as gr
 
 
 
 
 
2
 
3
  def display_html(html_code):
4
  return f"""
 
84
  }
85
  """
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
88
  gr.Markdown(
89
  """
 
98
  placeholder="ここにHTMLコードを貼り付けてください...",
99
  lines=10
100
  )
101
+ submit_button = gr.Button("表示", variant="primary") # スタイルを元に戻す
102
+
 
103
 
104
  with gr.Column(scale=3):
105
  output_html = gr.HTML(label="プレビュー")
 
 
106
 
107
  submit_button.click(
108
  display_html,
 
110
  outputs=output_html
111
  )
112
 
 
 
 
 
 
 
113
  #サンプル
114
  gr.Examples(
115
  [["<h1>Hello world</h1>"],["""