Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def display_html(html_code):
|
| 4 |
return f"""
|
|
@@ -7,6 +9,8 @@ def display_html(html_code):
|
|
| 7 |
</div>
|
| 8 |
"""
|
| 9 |
|
|
|
|
|
|
|
| 10 |
# カスタムテーマ (フォント、テキストスタイルを大幅改良)
|
| 11 |
theme = gr.themes.Default(
|
| 12 |
primary_hue=gr.themes.colors.indigo,
|
|
@@ -83,23 +87,40 @@ a:hover {
|
|
| 83 |
"""
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
|
| 87 |
gr.Markdown(
|
| 88 |
"""
|
| 89 |
# HTML Viewer
|
| 90 |
|
| 91 |
入力されたHTMLコードをプレビュー表示します。
|
| 92 |
-
""")
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
input_textbox = gr.Textbox(
|
| 96 |
-
label="HTMLコード",
|
| 97 |
placeholder="ここにHTMLコードを貼り付けてください...",
|
| 98 |
lines=10
|
| 99 |
)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
with gr.Column(scale=3):
|
| 102 |
-
output_html = gr.HTML(label="プレビュー"
|
|
|
|
| 103 |
|
| 104 |
submit_button.click(
|
| 105 |
display_html,
|
|
@@ -107,6 +128,12 @@ with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
|
|
| 107 |
outputs=output_html
|
| 108 |
)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
#サンプル
|
| 111 |
gr.Examples(
|
| 112 |
[["<h1>Hello world</h1>"],["""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import weasyprint
|
| 3 |
+
import base64 # PDFをData URLに変換するために必要
|
| 4 |
|
| 5 |
def display_html(html_code):
|
| 6 |
return f"""
|
|
|
|
| 9 |
</div>
|
| 10 |
"""
|
| 11 |
|
| 12 |
+
# (テーマとCSSは前の回答のものをそのまま使用)
|
| 13 |
+
# ... (テーマ設定, css変数) ...
|
| 14 |
# カスタムテーマ (フォント、テキストスタイルを大幅改良)
|
| 15 |
theme = gr.themes.Default(
|
| 16 |
primary_hue=gr.themes.colors.indigo,
|
|
|
|
| 87 |
"""
|
| 88 |
|
| 89 |
|
| 90 |
+
def generate_pdf(html_code):
|
| 91 |
+
"""
|
| 92 |
+
HTMLコードからPDFを生成し、Data URLとして返す。
|
| 93 |
+
"""
|
| 94 |
+
html = weasyprint.HTML(string=html_code)
|
| 95 |
+
pdf_bytes = html.write_pdf() # PDFをバイト列として生成
|
| 96 |
+
|
| 97 |
+
# バイト列をBase64エンコードし、Data URLを作成
|
| 98 |
+
pdf_base64 = base64.b64encode(pdf_bytes).decode('utf-8')
|
| 99 |
+
pdf_data_url = f"data:application/pdf;base64,{pdf_base64}"
|
| 100 |
+
return pdf_data_url
|
| 101 |
+
|
| 102 |
+
|
| 103 |
with gr.Blocks(theme=theme, title="HTML Viewer", css=css) as demo:
|
| 104 |
gr.Markdown(
|
| 105 |
"""
|
| 106 |
# HTML Viewer
|
| 107 |
|
| 108 |
入力されたHTMLコードをプレビュー表示します。
|
| 109 |
+
""")
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column(scale=1):
|
| 112 |
input_textbox = gr.Textbox(
|
| 113 |
+
label="HTMLコード",
|
| 114 |
placeholder="ここにHTMLコードを貼り付けてください...",
|
| 115 |
lines=10
|
| 116 |
)
|
| 117 |
+
with gr.Row():
|
| 118 |
+
submit_button = gr.Button("表示", variant="primary")
|
| 119 |
+
pdf_button = gr.Button("PDFで保存", variant="secondary") #PDFボタン追加
|
| 120 |
+
|
| 121 |
with gr.Column(scale=3):
|
| 122 |
+
output_html = gr.HTML(label="プレビュー")
|
| 123 |
+
|
| 124 |
|
| 125 |
submit_button.click(
|
| 126 |
display_html,
|
|
|
|
| 128 |
outputs=output_html
|
| 129 |
)
|
| 130 |
|
| 131 |
+
# PDFダウンロードボタンの処理
|
| 132 |
+
pdf_button.click(
|
| 133 |
+
generate_pdf,
|
| 134 |
+
inputs=input_textbox, #input_textboxをPDFのインプットにする
|
| 135 |
+
outputs=output_html #出力先をgr.htmlにする
|
| 136 |
+
)
|
| 137 |
#サンプル
|
| 138 |
gr.Examples(
|
| 139 |
[["<h1>Hello world</h1>"],["""
|