Today, testing textual python package with Julia fractal animation.
Textual is a Rapid Application Development framework for Python, built by Textualize.io.
Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal or a web browser!
See the official website.

Let's see the source code:
import math
from rich.color import Color
from rich.segment import Segment
from rich.style import Style
from textual.app import App, ComposeResult
from textual.strip import Strip
from textual.widget import Widget
from textual.widgets import Footer, Header
def hsl_to_rgb(h: float, s: float, l: float) -> tuple[int, int, int]:
"""Convertește coordonate HSL (0..1) în tuple RGB (0..255)."""
if s == 0.0:
v = int(l * 255)
return v, v, v
def hue_to_rgb(p: float, q: float, t: float) -> float:
if t < 0.0:
t += 1.0
if t > 1.0:
t -= 1.0
if t < 1.0 / 6.0:
return p + (q - p) * 6.0 * t
if t < 1.0 / 2.0:
return q
if t < 2.0 / 3.0:
return p + (q - p) * (2.0 / 3.0 - t) * 6.0
return p
q = l * (1.0 + s) if l < 0.5 else l + s - l * s
p = 2.0 * l - q
r = hue_to_rgb(p, q, h + 1.0 / 3.0)
g = hue_to_rgb(p, q, h)
b = hue_to_rgb(p, q, h - 1.0 / 3.0)
return int(r * 255), int(g * 255), int(b * 255)
class JuliaPixelCanvas(Widget):
"""Fiecare celulă din grilă este tratată ca un pixel pătrat (1 celulă = 1 eșantion)."""
DEFAULT_CSS = """
JuliaPixelCanvas {
width: 100%;
height: 100%;
}
"""
def __init__(self) -> None:
super().__init__()
self.angle: float = 0.0
self.zoom: float = 1.0
self.paused: bool = False
self.max_iter: int = 36
# Raport lățime/înălțime al unei celule din terminal (~0.5 pentru font monospace standard)
self.cell_ratio: float = 0.5
def on_mount(self) -> None:
self.set_interval(1 / 15, self.step_animation)
def step_animation(self) -> None:
if not self.paused:
self.angle = (self.angle + 0.045) % (2.0 * math.pi)
self.refresh()
def _cell_color(self, x: int, y: int, width: int, height: int, cx: float, cy: float) -> Color:
# Mapăm centrul fiecărei celule (x + 0.5, y + 0.5) în planul complex
# Folosim o scară comună pe ambele axe, corectată cu cell_ratio,
# astfel încât pașii în planul complex să fie pătrați (Δzx == Δzy)
min_dim = max(1, min(width * self.cell_ratio, height))
scale = 3.1 / (self.zoom * min_dim)
zx = ((x + 0.5) - width / 2.0) * (scale * self.cell_ratio)
zy = ((y + 0.5) - height / 2.0) * scale
iteration = 0
max_iter = self.max_iter
while zx * zx + zy * zy < 4.0 and iteration < max_iter:
xtemp = zx * zx - zy * zy + cx
zy = 2.0 * zx * zy + cy
zx = xtemp
iteration += 1
if iteration == max_iter:
return Color.from_rgb(10, 12, 20)
hue = ((iteration / max_iter) + self.angle / (2.0 * math.pi)) % 1.0
r, g, b = hsl_to_rgb(hue, 0.88, 0.54)
return Color.from_rgb(r, g, b)
def render_line(self, y: int) -> Strip:
width = self.size.width
height = self.size.height
if width <= 0 or height <= 0 or y >= height:
return Strip([])
cx = 0.7885 * math.cos(self.angle)
cy = 0.7885 * math.sin(self.angle)
segments: list[Segment] = []
for x in range(width):
color = self._cell_color(x, y, width, height, cx, cy)
# "█" (Full Block) face ca fiecare celulă să arate ca un bloc/pixel plin
segments.append(Segment("█", Style(color=color)))
return Strip(segments)
class JuliaPixelApp(App):
"""Aplicație Textual cu pătrate la nivel de celulă (pixel de terminal)."""
TITLE = "Julia Pixel TUI"
BINDINGS = [
("space", "toggle_pause", "Pauză"),
("plus", "zoom_in", "Zoom+"),
("equal", "zoom_in", "Zoom+"),
("minus", "zoom_out", "Zoom-"),
("left_square_bracket", "ratio_down", "Pătrat-"),
("right_square_bracket", "ratio_up", "Pătrat+"),
("r", "reset_view", "Reset"),
("q", "quit", "Ieșire"),
]
def compose(self) -> ComposeResult:
yield Header(show_clock=False)
yield JuliaPixelCanvas()
yield Footer()
def action_toggle_pause(self) -> None:
canvas = self.query_one(JuliaPixelCanvas)
canvas.paused = not canvas.paused
def action_zoom_in(self) -> None:
canvas = self.query_one(JuliaPixelCanvas)
canvas.zoom *= 1.25
canvas.max_iter = min(96, int(36 + math.log2(max(1.0, canvas.zoom)) * 10))
canvas.refresh()
def action_zoom_out(self) -> None:
canvas = self.query_one(JuliaPixelCanvas)
canvas.zoom = max(0.4, canvas.zoom / 1.25)
canvas.refresh()
def action_ratio_down(self) -> None:
canvas = self.query_one(JuliaPixelCanvas)
canvas.cell_ratio = max(0.25, round(canvas.cell_ratio - 0.05, 2))
canvas.refresh()
def action_ratio_up(self) -> None:
canvas = self.query_one(JuliaPixelCanvas)
canvas.cell_ratio = min(1.0, round(canvas.cell_ratio + 0.05, 2))
canvas.refresh()
def action_reset_view(self) -> None:
canvas = self.query_one(JuliaPixelCanvas)
canvas.zoom = 1.0
canvas.angle = 0.0
canvas.max_iter = 36
canvas.cell_ratio = 0.5
canvas.paused = False
canvas.refresh()
if __name__ == "__main__":
JuliaPixelApp().run()




