Commit
·
63a30f4
1
Parent(s):
eddcd94
Add Gradio web interface for code GIF generator
Browse files- .python-version +1 -0
- README.md +25 -0
- app.py +156 -0
- pyproject.toml +13 -0
- requirements.txt +5 -0
- uv.lock +0 -0
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.11
|
README.md
CHANGED
|
@@ -1,2 +1,27 @@
|
|
| 1 |
# python-code-gifs
|
|
|
|
| 2 |
Some code to create code gifs with Python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# python-code-gifs
|
| 2 |
+
|
| 3 |
Some code to create code gifs with Python
|
| 4 |
+
|
| 5 |
+
## Usage
|
| 6 |
+
|
| 7 |
+
```bash
|
| 8 |
+
python app.py
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
## install
|
| 12 |
+
|
| 13 |
+
Install the package with pip:
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
python -m venv .venv
|
| 17 |
+
source .venv/bin/activate
|
| 18 |
+
pip install .
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
Install the package with uv:
|
| 22 |
+
|
| 23 |
+
```bash
|
| 24 |
+
uv venv
|
| 25 |
+
source .venv/bin/activate
|
| 26 |
+
uv pip install .
|
| 27 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
from main import create_code_gif
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _create_code_gif(
|
| 10 |
+
code: str,
|
| 11 |
+
style_name: str = "monokai",
|
| 12 |
+
start_color: str = "#FF6B6B",
|
| 13 |
+
end_color: str = "#4ECDC4",
|
| 14 |
+
title: str = "Code GIF",
|
| 15 |
+
favicon: str = "https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
|
| 16 |
+
photo: str = "https://photo.jpg",
|
| 17 |
+
aspect_ratio: float = 16 / 9,
|
| 18 |
+
):
|
| 19 |
+
# Convert hex colors to RGB tuples
|
| 20 |
+
def parse_color(color: str) -> tuple:
|
| 21 |
+
if color.startswith("rgba"):
|
| 22 |
+
# Extract numbers from rgba(r, g, b, a) format
|
| 23 |
+
values = color.strip("rgba()").split(",")
|
| 24 |
+
return tuple(int(float(x)) for x in values[:3]) # Only use RGB values
|
| 25 |
+
else:
|
| 26 |
+
# Handle hex colors
|
| 27 |
+
return tuple(int(color.lstrip("#")[i : i + 2], 16) for i in (0, 2, 4))
|
| 28 |
+
|
| 29 |
+
start_rgb = parse_color(start_color)
|
| 30 |
+
end_rgb = parse_color(end_color)
|
| 31 |
+
|
| 32 |
+
with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as tmp:
|
| 33 |
+
create_code_gif(
|
| 34 |
+
code=code,
|
| 35 |
+
output_file=tmp.name,
|
| 36 |
+
style=style_name,
|
| 37 |
+
gradient_start=start_rgb,
|
| 38 |
+
gradient_end=end_rgb,
|
| 39 |
+
line_numbers=False,
|
| 40 |
+
title=title,
|
| 41 |
+
filename="code.py",
|
| 42 |
+
favicon=favicon,
|
| 43 |
+
photo=photo,
|
| 44 |
+
aspect_ratio=aspect_ratio,
|
| 45 |
+
)
|
| 46 |
+
return tmp.name
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
code_placeholder = """
|
| 50 |
+
def main():
|
| 51 |
+
print("Hello, World!")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
main()
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
def get_random_color():
|
| 61 |
+
return f"#{random.randint(0, 0xFFFFFF):06x}"
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
start_color = get_random_color()
|
| 65 |
+
end_color = get_random_color()
|
| 66 |
+
|
| 67 |
+
with gr.Blocks(title="Python Code GIF Generator") as demo:
|
| 68 |
+
gr.Markdown("# Python Code GIF Generator")
|
| 69 |
+
gr.Markdown("Generate animated code GIFs with customizable styles and effects")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
gr.Markdown("### Input Settings")
|
| 74 |
+
code = gr.Code(
|
| 75 |
+
label="Code", lines=10, language="python", value=code_placeholder
|
| 76 |
+
)
|
| 77 |
+
title = gr.Textbox(
|
| 78 |
+
label="Title", value="Code GIF", placeholder="My animated code"
|
| 79 |
+
)
|
| 80 |
+
with gr.Row(variant="compact"):
|
| 81 |
+
style = gr.Dropdown(
|
| 82 |
+
choices=[
|
| 83 |
+
"monokai",
|
| 84 |
+
"dracula",
|
| 85 |
+
"nord-darker",
|
| 86 |
+
"gruvbox-dark",
|
| 87 |
+
"solarized-dark",
|
| 88 |
+
"one-dark",
|
| 89 |
+
"github-dark",
|
| 90 |
+
"material",
|
| 91 |
+
"zenburn",
|
| 92 |
+
"vs-dark",
|
| 93 |
+
"tomorrow-night",
|
| 94 |
+
"paraiso-dark",
|
| 95 |
+
"native",
|
| 96 |
+
"fruity",
|
| 97 |
+
"vim",
|
| 98 |
+
],
|
| 99 |
+
label="Style",
|
| 100 |
+
value="monokai",
|
| 101 |
+
)
|
| 102 |
+
aspect_ratio = gr.Dropdown(
|
| 103 |
+
label="Aspect Ratio",
|
| 104 |
+
choices=[("Portrait (9:16)", 9 / 16), ("Landscape (16:9)", 16 / 9)],
|
| 105 |
+
value=9 / 16,
|
| 106 |
+
type="value",
|
| 107 |
+
)
|
| 108 |
+
with gr.Row(variant="compact"):
|
| 109 |
+
start_color_picker = gr.ColorPicker(
|
| 110 |
+
label="Start Color", value=start_color
|
| 111 |
+
)
|
| 112 |
+
end_color_picker = gr.ColorPicker(label="End Color", value=end_color)
|
| 113 |
+
refresh_colors = gr.Button("Refresh Colors")
|
| 114 |
+
with gr.Row():
|
| 115 |
+
photo = gr.Textbox(
|
| 116 |
+
label="Photo",
|
| 117 |
+
value="https://www.indiewire.com/wp-content/uploads/2017/10/matrix-code.jpg",
|
| 118 |
+
placeholder="https://www.indiewire.com/wp-content/uploads/2017/10/matrix-code.jpg",
|
| 119 |
+
)
|
| 120 |
+
favicon = gr.Textbox(
|
| 121 |
+
label="Favicon",
|
| 122 |
+
value="https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
|
| 123 |
+
placeholder="https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
|
| 124 |
+
)
|
| 125 |
+
with gr.Row():
|
| 126 |
+
submit_btn = gr.Button("Generate GIF")
|
| 127 |
+
|
| 128 |
+
# Right column - Output display
|
| 129 |
+
with gr.Column():
|
| 130 |
+
gr.Markdown("### Generated Output")
|
| 131 |
+
output_image = gr.Image(label="Generated GIF", type="filepath")
|
| 132 |
+
|
| 133 |
+
submit_btn.click(
|
| 134 |
+
fn=_create_code_gif,
|
| 135 |
+
inputs=[
|
| 136 |
+
code,
|
| 137 |
+
style,
|
| 138 |
+
start_color_picker,
|
| 139 |
+
end_color_picker,
|
| 140 |
+
title,
|
| 141 |
+
favicon,
|
| 142 |
+
photo,
|
| 143 |
+
aspect_ratio,
|
| 144 |
+
],
|
| 145 |
+
outputs=output_image,
|
| 146 |
+
)
|
| 147 |
+
gr.on(
|
| 148 |
+
[refresh_colors.click, demo.load],
|
| 149 |
+
fn=lambda: (get_random_color(), get_random_color()),
|
| 150 |
+
inputs=[],
|
| 151 |
+
outputs=[start_color_picker, end_color_picker],
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
demo.launch()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "python-code-gifs"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Create animated code gifs"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.11"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"cairosvg>=2.7.1",
|
| 9 |
+
"gradio>=5.13.2",
|
| 10 |
+
"numpy>=2.2.2",
|
| 11 |
+
"pillow>=11.1.0",
|
| 12 |
+
"pygments>=2.19.1",
|
| 13 |
+
]
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cairosvg
|
| 2 |
+
gradio
|
| 3 |
+
numpy
|
| 4 |
+
pillow
|
| 5 |
+
pygments
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|