When button click on Anvil Client side display (access a *.png) from Colab server side
This should be simple but I cant solve it, probably its my lack of understanding of things.
Here are the steps I have:
- Anvil client
- button to upload a dxf file on colab (working)
def file_loader_1_change(self, file, **event_args) - file gets saved on server with same name no mater what the original file name is
on the server its always saved under: “dxf_for_ezdxf.dxf” (working) - on client I have a image holder
self.image_1.source - and I have a button
def check_dxf_1_click(self, **event_args)
Code Sample:
from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
import anvil.media
from anvil import app
class Form1(Form1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
self.uploaded_file = None
def file_loader_1_change(self, file, **event_args):
"""This method is called when a new file is loaded into this FileLoader"""
file_name = file.name
parsed_file_name = f"dxf_for_ezdxf.dxf"#self.parse_file_name(file_name)
self.uploaded_file = file
anvil.server.call("uplink_download", file, parsed_file_name)
def check_dxf_1_click(self, **event_args):
"""This method is called when the button is clicked"""
if not self.uploaded_file:
return
parsed_file_name = f"dxf_for_ezdxf.dxf"
polygons, circles = anvil.server.call('get_dxf_data', f"dxf_for_ezdxf.dxf")
# Plot the data
fig, ax = anvil.mpl_util.create_plot(figsize=(10, 10))
for polygon in polygons:
x, y = polygon.exterior.xy
ax.plot(x, y, color='black')
for circle in circles:
x, y = circle.boundary.xy
ax.plot(x, y, color='green', linestyle='-', linewidth=1)
# Set the plot as the image source
self.image_1.source = anvil.mpl_util.plot_image(fig)
- Colab Server side
-
I get the dxf file and save it with same name
“dxf_for_ezdxf.dxf” -
I parse the dxf file to extract data and plot with matplotlib (working on server)
-
I save the plot as a *.png file with same name “dxf_for_ezdxf.png”
-
I try to use anvil.mpl_util to generate the plot for Anvil client unsuccessfull
-
I cant retrieve neither the *.png from the server
Code Sample:
@anvil.server.callable
def uplink_download(media, file_name):
with open(file_name, 'wb') as f:
f.write(media.get_bytes())
import anvil.mpl_util
#I cant use this library on Anvil side as I am on the free tier
@anvil.server.callable
def get_dxf_data(file):
dxf = ezdxf.readfile(file.name)
msp = dxf.modelspace()
polygons = []
circles = []
for entity in msp:
if entity.dxftype() == 'POLYLINE':
polygon = shapely.geometry.Polygon([(vertex.dxf.location[0], vertex.dxf.location[1]) for vertex in entity.vertices()])
polygons.append(polygon)
elif entity.dxftype() == 'CIRCLE':
center = entity.dxf.center
radius = entity.dxf.radius
circle = shapely.geometry.Point(center[0], center[1]).buffer(radius)
circles.append(circle)
return polygons, circles
@anvil.server.callable
def plot_polygons(polygons, circles):
fig = plt.figure(figsize=(10, 10))
ax = plt.axes()
for polygon in polygons:
x, y = polygon.exterior.xy
ax.plot(x, y, color='black')
for circle in circles:
x, y = circle.boundary.xy
ax.plot(x, y, color='green', linestyle='-', linewidth=1)
# Convert the plot to a PNG image
img = anvil.mpl_util.plot_image(fig)
return img
@anvil.server.callable
def get_png_image():
with open('dxf_for_ezdxf.png', 'rb') as f:
return f.read
So, how do I retrieve a image from the server to the client when I click a button on the Anvil client side?
I really appreciate all the help I can get!
It should be a simple task but I cant manage to have it working.
PS. anvil.mpl_util - I cant use it on Anvil side as I’m on the free tier till I get the app concept working
so my option now is to retrieve the *.png image from server side directly