Getting Hyperlink of image or uploading to Imgur?

If you’re working with data tables, you can store a datetime in the database. Then, each time you (eg) upload a new image, you can query all the images in the database (ordered by time, eg tables.order_by('upload_date')), and delete anything that’s too old.

Alternatively, if you do want to use the Imgur API, email support@anvil.works to get the imgurpython module installed in your server modules. You can then pass a Media object to a server function (from a FileLoader component), dump the file out to a temporary file using get_bytes(), then feed it into the API. Something like this, perhaps:

In a form:

  # ...on the client
  def button_1_click(self, **event_args):
    anvil.server.call("upload_img", self.file_loader_1.file)

In a server module:

@anvil.server.callable
def upload_img(img):
  filename = '/tmp/upload_%s' % random.SystemRandom().random()
  try:
    with open(filename, 'w') as f:
      f.write(img.get_bytes())
    # Do your imgur upload here.
    imgurpython.do_something_with_a_file(filename)
  finally:
    os.unlink(filename)

Hope that helps!

1 Like