@@ -7,9 +7,9 @@ This example demonstrates rendering HTML tables:
|
||||
- Tables with multiple rows and columns
|
||||
- Tables with colspan and borders
|
||||
- Tables with formatted content
|
||||
- Tables parsed from HTML
|
||||
- Tables parsed from HTML using DocumentLayouter
|
||||
|
||||
Shows the TableRenderer system in action.
|
||||
Shows the HTML-first rendering pipeline.
|
||||
"""
|
||||
|
||||
import sys
|
||||
@@ -20,8 +20,9 @@ from PIL import Image, ImageDraw
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from pyWebLayout.concrete.page import Page
|
||||
from pyWebLayout.concrete.table import TableRenderer, TableStyle
|
||||
from pyWebLayout.concrete.table import TableStyle
|
||||
from pyWebLayout.style.page_style import PageStyle
|
||||
from pyWebLayout.layout.document_layouter import DocumentLayouter
|
||||
from pyWebLayout.io.readers.html_extraction import parse_html_string
|
||||
from pyWebLayout.style import Font
|
||||
from pyWebLayout.abstract.block import Table
|
||||
@@ -179,7 +180,7 @@ def create_data_table_example():
|
||||
|
||||
|
||||
def render_table_example(html: str, title: str, style_variant: int = 0, page_size=(500, 400)):
|
||||
"""Render a table from HTML to an image."""
|
||||
"""Render a table from HTML to an image using DocumentLayouter."""
|
||||
# Create page with varying backgrounds
|
||||
bg_colors = [
|
||||
(255, 255, 255), # White
|
||||
@@ -196,17 +197,6 @@ def render_table_example(html: str, title: str, style_variant: int = 0, page_siz
|
||||
)
|
||||
|
||||
page = Page(size=page_size, style=page_style)
|
||||
image = page.render()
|
||||
draw = ImageDraw.Draw(image)
|
||||
|
||||
# Add title
|
||||
from PIL import ImageFont
|
||||
try:
|
||||
title_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 14)
|
||||
except:
|
||||
title_font = ImageFont.load_default()
|
||||
|
||||
draw.text((page.border_size + 10, page.border_size + 10), title, fill=(50, 50, 150), font=title_font)
|
||||
|
||||
# Parse HTML to get table
|
||||
base_font = Font(font_size=12)
|
||||
@@ -219,67 +209,66 @@ def render_table_example(html: str, title: str, style_variant: int = 0, page_siz
|
||||
table = block
|
||||
break
|
||||
|
||||
# Table styles with different themes
|
||||
table_styles = [
|
||||
# Style 0: Classic blue header
|
||||
TableStyle(
|
||||
border_width=1,
|
||||
border_color=(80, 80, 80),
|
||||
cell_padding=(8, 10, 8, 10),
|
||||
header_bg_color=(70, 130, 180), # Steel blue
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=(240, 248, 255) # Alice blue
|
||||
),
|
||||
# Style 1: Green theme
|
||||
TableStyle(
|
||||
border_width=2,
|
||||
border_color=(34, 139, 34), # Forest green
|
||||
cell_padding=(10, 12, 10, 12),
|
||||
header_bg_color=(144, 238, 144), # Light green
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=(240, 255, 240) # Honeydew
|
||||
),
|
||||
# Style 2: Minimal style
|
||||
TableStyle(
|
||||
border_width=0,
|
||||
border_color=(200, 200, 200),
|
||||
cell_padding=(6, 8, 6, 8),
|
||||
header_bg_color=(245, 245, 245),
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=None # No alternating
|
||||
),
|
||||
# Style 3: Bold borders
|
||||
TableStyle(
|
||||
border_width=3,
|
||||
border_color=(0, 0, 0),
|
||||
cell_padding=(10, 10, 10, 10),
|
||||
header_bg_color=(255, 215, 0), # Gold
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=(255, 250, 205) # Lemon chiffon
|
||||
),
|
||||
]
|
||||
|
||||
table_style = table_styles[style_variant % len(table_styles)]
|
||||
|
||||
if table:
|
||||
# Create table renderer with different styles
|
||||
table_styles = [
|
||||
# Style 0: Classic blue header
|
||||
TableStyle(
|
||||
border_width=1,
|
||||
border_color=(80, 80, 80),
|
||||
cell_padding=(8, 10, 8, 10),
|
||||
header_bg_color=(70, 130, 180), # Steel blue
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=(240, 248, 255) # Alice blue
|
||||
),
|
||||
# Style 1: Green theme
|
||||
TableStyle(
|
||||
border_width=2,
|
||||
border_color=(34, 139, 34), # Forest green
|
||||
cell_padding=(10, 12, 10, 12),
|
||||
header_bg_color=(144, 238, 144), # Light green
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=(240, 255, 240) # Honeydew
|
||||
),
|
||||
# Style 2: Minimal style
|
||||
TableStyle(
|
||||
border_width=0,
|
||||
border_color=(200, 200, 200),
|
||||
cell_padding=(6, 8, 6, 8),
|
||||
header_bg_color=(245, 245, 245),
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=None # No alternating
|
||||
),
|
||||
# Style 3: Bold borders
|
||||
TableStyle(
|
||||
border_width=3,
|
||||
border_color=(0, 0, 0),
|
||||
cell_padding=(10, 10, 10, 10),
|
||||
header_bg_color=(255, 215, 0), # Gold
|
||||
cell_bg_color=(255, 255, 255),
|
||||
alternate_row_color=(255, 250, 205) # Lemon chiffon
|
||||
),
|
||||
]
|
||||
# Create DocumentLayouter
|
||||
layouter = DocumentLayouter(page)
|
||||
|
||||
table_style = table_styles[style_variant % len(table_styles)]
|
||||
# Use DocumentLayouter to layout the table
|
||||
layouter.layout_table(table, style=table_style)
|
||||
|
||||
# Position table below title
|
||||
table_origin = (page.border_size + 10, page.border_size + 40)
|
||||
table_width = page.content_size[0] - 20
|
||||
|
||||
renderer = TableRenderer(
|
||||
table,
|
||||
table_origin,
|
||||
table_width,
|
||||
draw,
|
||||
table_style
|
||||
)
|
||||
|
||||
renderer.render()
|
||||
# Get the rendered canvas
|
||||
_ = page.draw # Ensure canvas exists
|
||||
image = page._canvas
|
||||
else:
|
||||
# Draw "No table found" message
|
||||
# No table found - create empty page
|
||||
_ = page.draw # Ensure canvas exists
|
||||
draw = ImageDraw.Draw(page._canvas)
|
||||
draw.text((page.border_size + 10, page.border_size + 50),
|
||||
"No table found in HTML",
|
||||
fill=(200, 0, 0), font=title_font)
|
||||
fill=(200, 0, 0))
|
||||
image = page._canvas
|
||||
|
||||
return image
|
||||
|
||||
|
||||
Reference in New Issue
Block a user