receive formdata and convert to int

This commit is contained in:
waltem01 2023-11-24 07:06:43 +01:00
parent 1a1744cf35
commit e3d6c64bd2

View File

@ -20,11 +20,11 @@ def display_text():
global matrix global matrix
# receive client data # receive client data
data = request.get_json() data = request.form
# try unpacking text and x,y coordinates # try unpacking text and x,y coordinates
text = data.get('text') text = data.get('text')
x_coord = data.get('x') x_coord = int(data.get('x'))
y_coord = data.get('y') y_coord = int(data.get('y'))
# call matrix method with data # call matrix method with data
matrix.text(x_coord, y_coord, text) matrix.text(x_coord, y_coord, text)
@ -45,10 +45,10 @@ def set_pixel():
global matrix global matrix
# receive client data # receive client data
data = request.get_json() data = request.form
# try unpacking x,y coordinates # try unpacking x,y coordinates
x_coord = data.get('x') x_coord = int(data.get('x'))
y_coord = data.get('y') y_coord = int(data.get('y'))
# call matrix method with data # call matrix method with data
matrix.pixel(x_coord, y_coord) matrix.pixel(x_coord, y_coord)
@ -69,11 +69,11 @@ def draw_circle():
global matrix global matrix
# receive client data # receive client data
data = request.get_json() data = request.form
# try unpacking radius and x,y coordinates # try unpacking radius and x,y coordinates
radius = data.get('r') radius = int(data.get('r'))
x_coord = data.get('x') x_coord = int(data.get('x'))
y_coord = data.get('y') y_coord = int(data.get('y'))
# call matrix method with data # call matrix method with data
matrix.circle(x_coord, y_coord, radius) matrix.circle(x_coord, y_coord, radius)
@ -94,13 +94,13 @@ def draw_rectangle():
global matrix global matrix
# receive client data # receive client data
data = request.get_json() data = request.form
# try unpacking rectangle width and height # try unpacking rectangle width and height
width = data.get('w') width = int(data.get('w'))
height = data.get('h') height = int(data.get('h'))
# try unpacking x,y coordinates # try unpacking x,y coordinates
x_coord = data.get('x') x_coord = int(data.get('x'))
y_coord = data.get('y') y_coord = int(data.get('y'))
# call matrix method with data # call matrix method with data
matrix.rectangle(x_coord, y_coord, width, height) matrix.rectangle(x_coord, y_coord, width, height)
@ -121,11 +121,11 @@ def set_color():
global matrix global matrix
# receive client data # receive client data
data = request.get_json() data = request.form
# try unpacking red, green and blue color values # try unpacking red, green and blue color values
red = data.get('r') red = int(data.get('r'))
green = data.get('g') green = int(data.get('g'))
blue = data.get('b') blue = int(data.get('b'))
# call matrix method with data # call matrix method with data
matrix.set_color(red, green, blue) matrix.set_color(red, green, blue)
@ -145,7 +145,7 @@ def clear_canvas():
try: try:
global matrix global matrix
# receive client data # clear matrix canvas
matrix.clear() matrix.clear()
except Exception as e: except Exception as e:
# error handling # error handling
@ -160,6 +160,8 @@ def update_matrix():
response = { 'success': True } response = { 'success': True }
try: try:
global matrix global matrix
# swap matrix canvas
matrix.update() matrix.update()
except Exception as e: except Exception as e:
print(e) print(e)
@ -200,23 +202,23 @@ class Matrix(SampleBase):
self.clear() self.clear()
# set current color # set current color
def set_color(self, r, g, b): def set_color(self, r: int, g: int, b: int):
self.color = graphics.Color(r, g, b) self.color = graphics.Color(r, g, b)
# set specified pixel to current color # set specified pixel to current color
def pixel(self, x, y): def pixel(self, x: int, y: int):
self.canvas.SetPixel(x, y, self.color.red, self.color.green, self.color.blue) self.canvas.SetPixel(x, y, self.color.red, self.color.green, self.color.blue)
# display text at position with current color # display text at position with current color
def text(self, x, y, t): def text(self, x: int, y: int, t: int):
graphics.DrawText(self.canvas, self.font, x*9, (y+1)*18, self.color, t) graphics.DrawText(self.canvas, self.font, x*9, (y+1)*18, self.color, t)
# display circle at position with radius in current color # display circle at position with radius in current color
def circle(self, x, y, r): def circle(self, x: int, y: int, r: int):
graphics.DrawCircle(self.canvas, self.font, x, y, r, self.color) graphics.DrawCircle(self.canvas, self.font, x, y, r, self.color)
# display rectangle at position with dimensions in current color # display rectangle at position with dimensions in current color
def rectangle(self, x, y, w, h): def rectangle(self, x: int, y: int, w: int, h: int):
# loop through each point in dimensions # loop through each point in dimensions
for i in range(w): for i in range(w):
for j in range(h): for j in range(h):