Spaces:
Sleeping
Sleeping
Update image in real time
Browse files- app.py +51 -31
- requirements.txt +53 -2
app.py
CHANGED
@@ -13,36 +13,56 @@ def html_to_image(html_code, width, height):
|
|
13 |
image = Image.open(BytesIO(imgkit.from_string(html_code, False, options=options)))
|
14 |
return image
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
["<div style='background: linear-gradient(45deg, #ff6b6b, #4ecdc4); padding: 20px; border-radius: 10px;'><h1 style='color: white; font-family: Arial;'>Hello, World!</h1></div>", 800, 400],
|
42 |
-
["<div style='background: #f0f0f0; padding: 20px;'><ul style='color: #333;'><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul></div>", 600, 300]
|
43 |
-
],
|
44 |
-
theme=gr.themes.Soft()
|
45 |
-
)
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
if __name__ == "__main__":
|
48 |
-
interface.launch()
|
|
|
13 |
image = Image.open(BytesIO(imgkit.from_string(html_code, False, options=options)))
|
14 |
return image
|
15 |
|
16 |
+
# Create the Gradio interface
|
17 |
+
with gr.Blocks() as interface:
|
18 |
+
gr.Markdown("# HTML to Image Converter")
|
19 |
+
gr.Markdown("Enter HTML code and set dimensions to generate an image")
|
20 |
+
with gr.Row():
|
21 |
+
with gr.Column():
|
22 |
+
html_code_input = gr.Code(
|
23 |
+
label="HTML Code",
|
24 |
+
language="html",
|
25 |
+
lines=30,
|
26 |
+
interactive=True # Make the code input interactive
|
27 |
+
)
|
28 |
+
width_input = gr.Number(
|
29 |
+
label="Width",
|
30 |
+
value=1280,
|
31 |
+
step=10,
|
32 |
+
info="Width in pixels"
|
33 |
+
)
|
34 |
+
height_input = gr.Number(
|
35 |
+
label="Height",
|
36 |
+
value=720,
|
37 |
+
step=10,
|
38 |
+
info="Height in pixels"
|
39 |
+
)
|
40 |
+
output_image = gr.Image(type="pil", format="PNG", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Trigger the function when the HTML Code input changes
|
43 |
+
html_code_input.change(
|
44 |
+
fn=html_to_image,
|
45 |
+
inputs=[html_code_input, width_input, height_input],
|
46 |
+
outputs=output_image
|
47 |
+
)
|
48 |
+
|
49 |
+
# Alternatively, trigger function on button click (optional)
|
50 |
+
generate_button = gr.Button("Refresh")
|
51 |
+
generate_button.click(
|
52 |
+
fn=html_to_image,
|
53 |
+
inputs=[html_code_input, width_input, height_input],
|
54 |
+
outputs=output_image
|
55 |
+
)
|
56 |
+
|
57 |
+
# Add examples
|
58 |
+
gr.Examples(
|
59 |
+
examples=[
|
60 |
+
["<div style='background: linear-gradient(45deg, #ff6b6b, #4ecdc4); padding: 20px; border-radius: 10px;'><h1 style='color: white; font-family: Arial;'>Hello, World!</h1></div>", 800, 400],
|
61 |
+
["<div style='background: #f0f0f0; padding: 20px;'><ul style='color: #333;'><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul></div>", 600, 300]
|
62 |
+
],
|
63 |
+
inputs=[html_code_input, width_input, height_input]
|
64 |
+
)
|
65 |
+
|
66 |
+
# Launch the Gradio app
|
67 |
if __name__ == "__main__":
|
68 |
+
interface.launch()
|
requirements.txt
CHANGED
@@ -1,2 +1,53 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.6.2.post1
|
4 |
+
certifi==2024.8.30
|
5 |
+
charset-normalizer==3.4.0
|
6 |
+
click==8.1.7
|
7 |
+
exceptiongroup==1.2.2
|
8 |
+
fastapi==0.115.5
|
9 |
+
ffmpy==0.4.0
|
10 |
+
filelock==3.16.1
|
11 |
+
fsspec==2024.10.0
|
12 |
+
gradio==5.5.0
|
13 |
+
gradio_client==1.4.2
|
14 |
+
h11==0.14.0
|
15 |
+
httpcore==1.0.6
|
16 |
+
httpx==0.27.2
|
17 |
+
huggingface-hub==0.26.2
|
18 |
+
idna==3.10
|
19 |
+
imgkit==1.2.3
|
20 |
+
Jinja2==3.1.4
|
21 |
+
markdown-it-py==3.0.0
|
22 |
+
MarkupSafe==2.1.5
|
23 |
+
mdurl==0.1.2
|
24 |
+
numpy==2.1.3
|
25 |
+
orjson==3.10.11
|
26 |
+
packaging==24.2
|
27 |
+
pandas==2.2.3
|
28 |
+
pillow==11.0.0
|
29 |
+
pydantic==2.9.2
|
30 |
+
pydantic_core==2.23.4
|
31 |
+
pydub==0.25.1
|
32 |
+
Pygments==2.18.0
|
33 |
+
python-dateutil==2.9.0.post0
|
34 |
+
python-multipart==0.0.12
|
35 |
+
pytz==2024.2
|
36 |
+
PyYAML==6.0.2
|
37 |
+
requests==2.32.3
|
38 |
+
rich==13.9.4
|
39 |
+
ruff==0.7.3
|
40 |
+
safehttpx==0.1.1
|
41 |
+
semantic-version==2.10.0
|
42 |
+
shellingham==1.5.4
|
43 |
+
six==1.16.0
|
44 |
+
sniffio==1.3.1
|
45 |
+
starlette==0.41.2
|
46 |
+
tomlkit==0.12.0
|
47 |
+
tqdm==4.67.0
|
48 |
+
typer==0.13.0
|
49 |
+
typing_extensions==4.12.2
|
50 |
+
tzdata==2024.2
|
51 |
+
urllib3==2.2.3
|
52 |
+
uvicorn==0.32.0
|
53 |
+
websockets==12.0
|