What I want to do:
I want to add a link to a grid table, this link should open a new tab in the browser and show the file stored in a table.
What I’ve tried and what’s not working:
I’ve currently achieved to download the file with this code:
# Function that downloads the html_body as file
def html_link_click(self, **properties):
if self.item['html_body'] is not None:
anvil.media.download(self.item['html_body'])
else:
Notification("HTML file does not exist.",
style="danger",
timeout=4,
).show()
But when I tried to follow this example Multiple upload of images and open each of the images in new tab to open the file in a new tab instead of download the file, nothing happens:
# Function that opens the html_body in a new tab
def html_link_click(self, **properties):
if self.item['html_body'] is not None:
self.item['html_body'].get_url(True)
else:
Notification("HTML file does not exist.",
style="danger",
timeout=4,
).show()
Thanks
You’re getting the URL, but not doing anything with it. There are a couple of ways of opening that URL in a new tab.
One is to set the URL property of the link to the media URL, and then don’t handle the link’s click event. The link will automatically open its URL in a new tab.
Another is to call Javascript to open the URL in a new tab, something like, anvil.js.window.open(url, '_blank')
1 Like
Hi @jshaffstall, thank you for your help. I tried both ways.
For the first one I don’t know how to set the URL to the Lazy Media.
This is what I tried:
url = self.item['html_body'].get_url(True)
But I get an error:
This is the url I get: https://sl2sdmv7ptv6x3wd.anvil.app/debug/BXPTWVF23DNT2ZLHICHIHQFR6OKCETAJ%3DKG64JNUKPPWJNQX4AYF2VJJW/url
How should I set the URL?
For the second one, it works nicely.
def html_link_click(self, **properties):
if self.item['html_body'] is not None:
url = self.item['html_body'].get_url(False)
anvil.js.window.open(url, '_blank')
Thank you again.
To set the url of the link, you have to reference the link component, e.g.
self.html_link.url = self.item['html_body'].get_url(True)
But that code would need to be in the repeating panel template form’s __init__
function to set the url before the user has a chance to click on the link.
But if the Javascript technique works, you don’t need to worry about the other method.
Thank you so much @jshaffstall. It works!!!