Skip to content

Commit

Permalink
Add a simple rendering function (most of it written by ChatGPT)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Apr 19, 2024
1 parent 5d07a2a commit f46a836
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
5 changes: 4 additions & 1 deletion python/examples/drawing/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import json
import sys

import schema as drawing
from dotenv import dotenv_values

import schema as drawing
from render import render_drawing

Check failure on line 8 in python/examples/drawing/demo.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of "render_drawing" is partially unknown   Type of "render_drawing" is "(drawing: Unknown) -> None" (reportUnknownVariableType)

from typechat import Success, Failure, TypeChatJsonTranslator, TypeChatValidator, PromptSection, create_language_model, process_requests

Check failure on line 10 in python/examples/drawing/demo.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Import "PromptSection" is not accessed (reportUnusedImport)


Expand Down Expand Up @@ -32,6 +34,7 @@ async def request_handler(request: str):
print(item["text"])
else:
history.append((request, output))
render_drawing(value)

await process_requests("~> ", file_path, request_handler)

Expand Down
33 changes: 19 additions & 14 deletions python/examples/drawing/render.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import tkinter as tk
from tkinter import Canvas

def render_drawing(drawing):

Check failure on line 3 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of parameter "drawing" is unknown (reportUnknownParameterType)

Check failure on line 3 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type annotation is missing for parameter "drawing" (reportMissingParameterType)
# Create a new Tkinter window
window = tk.Tk()
window.title("Drawing")
window.configure(bg='white') # Set the background color of the window

# Create a canvas widget
canvas = Canvas(window, width=800, height=600, bg='white')
canvas.pack()
canvas = tk.Canvas(window, width=800, height=600, bg='white', highlightthickness=0)
canvas.pack(padx=10, pady=10) # Adds 10 pixels of padding on all sides

# Function to draw a box with text if provided
def draw_box(box):

Check failure on line 14 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of parameter "box" is unknown (reportUnknownParameterType)

Check failure on line 14 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type annotation is missing for parameter "box" (reportMissingParameterType)
x1, y1 = box['x'], box['y']

Check failure on line 15 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of "x1" is unknown (reportUnknownVariableType)

Check failure on line 15 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of "y1" is unknown (reportUnknownVariableType)
x2, y2 = x1 + box['width'], y1 + box['height']

Check failure on line 16 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of "x2" is unknown (reportUnknownVariableType)

Check failure on line 16 in python/examples/drawing/render.py

View workflow job for this annotation

GitHub Actions / pyright (ubuntu-latest, 3.11)

Type of "y2" is unknown (reportUnknownVariableType)
fill = box['style'].get('fill_color', '') if 'style' in box else ''
fill = box['style'].get('fill_color', '') if box['style'] else ''
canvas.create_rectangle(x1, y1, x2, y2, outline='black', fill=fill)
if 'text' in box and box['text']:
canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2, text=box['text'], fill='black')
Expand All @@ -32,7 +32,7 @@ def draw_ellipse(ellipse):
def draw_arrow(arrow):
x1, y1 = arrow['start_x'], arrow['start_y']
x2, y2 = arrow['end_x'], arrow['end_y']
canvas.create_line(x1, y1, x2, y2, arrow=tk.LAST)
canvas.create_line(x1, y1, x2, y2, arrow=tk.LAST, fill='black')

# Iterate through each item in the drawing and render it
for item in drawing['items']:
Expand All @@ -43,16 +43,21 @@ def draw_arrow(arrow):
elif item['type'] == 'Arrow':
draw_arrow(item)

# Button to close the window (pretty ugly -- use Cmd-W/Ctrl-W instead)
# quit_button = tk.Button(window, text="Quit", command=window.quit)
# quit_button.pack(side=tk.BOTTOM, pady=10)

# Start the Tkinter event loop
window.mainloop()

# Example usage:
drawing = {
'items': [
{'type': 'Box', 'x': 50, 'y': 50, 'width': 100, 'height': 100, 'text': 'Hello'},
{'type': 'Ellipse', 'x': 200, 'y': 50, 'width': 150, 'height': 100, 'text': 'World', 'style': {'fill_color': 'lightblue'}},
{'type': 'Arrow', 'start_x': 50, 'start_y': 200, 'end_x': 150, 'end_y': 200}
]
}

render_drawing(drawing)
if __name__ == '__main__':
drawing = {
'items': [
{'type': 'Box', 'x': 50, 'y': 50, 'width': 100, 'height': 100, 'text': 'Hello', 'style': None},
{'type': 'Ellipse', 'x': 200, 'y': 50, 'width': 150, 'height': 100, 'text': 'World', 'style': {'fill_color': 'lightblue'}},
{'type': 'Arrow', 'start_x': 50, 'start_y': 200, 'end_x': 150, 'end_y': 200}
]
}

render_drawing(drawing)

0 comments on commit f46a836

Please sign in to comment.