I have been trying to display a dataframe where a column consists a link to download a .pdf file from the local file system in streamlit. I have the link showing up perfectly fine in streamlit app but when I click on the link nothing seems to happen. I have followed this question Question, which was asked previously but looks like I am making a mistake somewhere. Any help on this is greatly appreciated.
This is my code below:
import os as o
import pandas as pd
import streamlit as st
df = pd.DataFrame({
'name': ['John', 'Mathew', 'Sato']
'link': ['C://Users//Satokuzuma//Documents//sample1.pdf', 'C://Users//Satokuzuma//Documents//sample2.pdf', 'C://Users//Satokuzuma//Documents//sample3.pdf']
})
def make_clickable(url, name):
return 'DN'.format(url, name)
def fun(path):
f_url = o.path.basename(path)
return '<a target="_blank" href="{}" download>{}</a>'.format(path, f_url)
df = df.style.format({'link' : fun})
st.write(df.to_html(render_links=True,escape=False), unsafe_allow_html=True)
With Results in Streamlit app as Below:
Results
When the link is clicked nothing happens no error or pop-up. My aim here is for the file to get downloaded when the clicked on the link. Any help or direction on this is greatly appreciated.
Tried to make a clickable hyperlink to download a .pdf located in a local system folder. Everything seems to work but when clicked on the hyperlink nothing happens.
Related
In a piece of code I am working on, I have created 2 ways to print the link for a song on spotify, should it exist. The first of which gives the link to the web based player, the second gives the format for a link that will try to open an installed app. However, only the printed link for the web player is clickable, as it is a http://www. type of link. Is there a way to format the print function to turn it into a link, so that way I could click on it in the output and try to open the spotify app?
try:
print("Spotify Web Player: " + "https://open.spotify.com/track/" + data['metadata']['music'][0]['external_metadata']['spotify']['track']['id'])
print("Spotify App: " + "spotify:track:" + data['metadata']['music'][0]['external_metadata']['spotify']['track']['id'])
Note, the direct app link looks like this, and if you paste it into the URL bar, it asks to open the app: spotify:track:60jpDubMmVyR5molJp2TCm
You can achieve this using the os module built into Python:
import os
os.startfile("spotify:track:60jpDubMmVyR5molJp2TCm")
This will successfully open the song inside of Spotify.
I'm writing a Python script that creates a COVID-19 dashboard for my country and state and updates it daily.
However, I am struggling to download one of the necessary files.
Basically to download the file I have to access the website (https://covid.saude.gov.br/) and click on a button (class="btn-white md button button-solid button-has-icon-only ion-activatable ion-focusable hydrated ion-activated").
I tried to download via the download link but the site creates a different link every time you click the button and it still has a blob URL before HTTP.
I am very grateful to anyone who tries to help, because the data will be used to monitor the progress of the disease here where I live.
You can use their API to get the file name:
import requests
headers = {
'authority':'xx9p7hp1p7.execute-api.us-east-1.amazonaws.com',
'x-parse-application-id':'unAFkcaNDeXajurGB7LChj8SgQYS2ptm',
}
with requests.Session() as session:
session.headers.update(headers)
resp = session.get('https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral').json()
path = resp['results'][0]['arquivo']['url']
The x-parse-application-id doesn't seem to change. If it does, you can get the correct one by querying https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeralApi and extract it from ['planilha']['arquivo'][url].
I am creating a Python script for downloading a report from Salesforce as a CSV.
My script is working perfectly fine for Salesforce Classic. However, I need to get it working for Lightning Experience. I'm using the simple-salesforce Python package to access our org. For SF Classic I enter a link that is structured like this: https://my-company.my.salesforce.com/my_report_id?view=d&snip&export=1&enc=UTF-8&xf=csv
The script is basically like this:
from simple-salesforce import Salesforce
import requests
import pandas as pd
import csv
from io import StringIO
sf = Salesforce(username="my_username", password="my_password",
security_token="my_token")
sf_org = "https://my_company.my.salesforce.com/"
report_id = "0000" # Some report id
sf_report_loc = "{0}{1}?view=d&snip&export=1&enc=UTF-8&xf=csv".format(sf_org, report_id)
response = requests.get(sf_report_loc, headers=sf.headers, cookies={"sid": sf.session_id})
new_report = response.content.decode("utf-8")
df = pd.read_csv(StringIO(new_report)) # Save the report to a DataFrame.
Whenever I switch to Lightning, the link is invalid and I get redirected. Is there a way to make this work in Lightning?
Try with isdtp parameter. In classic it was used to force view pages without sidebar or header, for example add isdtp=vw to a random page and see what happens.
https://my_company.my.salesforce.com/00O.....?isdtp=p1&export=1&enc=UTF-8&xf=csv ?
(no idea what's 'p1' but that's what I see in Chrome's download history as part of the report's source URL)
I am trying to upload image file into the browser using mechanize.
Although there is no error, the uploaded file does not reflect when I check manually in the browser (post submit/saving).
I am using the following code to upload the files
import mechanize as mc
br = mc.Browser()
br.set_handle_robots(False)
br.select_form(nr=0)
br.form.add_file(open("test.png"), content_type="image/png",
filename='before',name="ctl00$ContentPlaceHolder1$fileuploadBeforeimages")
br.submit("ctl00$ContentPlaceHolder1$cmdSave")
# this is supposed to save the form on the webpage. It saves the texts in the other fields, whereas the image does not show up.
The add file command seems to work. I can confirm this because when I print br.forms()[0] the file details show up (<FileControl(ctl00$ContentPlaceHolder1$fileuploadBeforeimages=before)>).
But there is no sign of the image file post this code snippet. I have checked several examples which include br.submit() without any specific button control, when I do this no page is saved on the website.
What am I missing?
Thanks in advance.
EDIT
When I manually try to upload the file, I see a pop-up asking for confirmation. Under inspect, this is present as
onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();"
I am not sure if this is a JavaScript element and mechanize cannot pass through this part for upload function. Can someone confirm this.?
you can just put 'rb' in front of image name like this:
br.form.add_file(open("test.png",'rb'),'images/png',filename,name='file')
I learned from this post that I can link to a website in a Jupyter Notebook:
How to create a table with clickable hyperlink in pandas & Jupyter Notebook
So, I tried to adapt the code to create a dataframe with links to local files. However, when I click on the hyperlinks from the code below, nothing happens.
How do I fix the code below for the hyperlinks to work?
import os
import pandas as pd
data = [dict(name='file1',
filepath='C:/Users/username/Documents/file1.docx'),
dict(name='file2',
filepath='C:/Users/username/Documents/file2.docx')]
df = pd.DataFrame(data)
def make_clickable(url):
name= os.path.basename(url)
return '{}'.format(url,name)
df.style.format({'filepath': make_clickable})
Your browser is actually blocking this. You probably see an error message like "Not allowed to load local resource" in your browser's developer tools (Chrome, Firefox, Safari). Changing this would expose you to serious security risks.
An alternative could be to put the files you want to access in the same working directory as your Jupyter Notebook. For instance, if you add a folder named "Documents" in your working directory, you can then link to the files like this:
http://localhost:8888/notebooks/Documents/file1.docx
Your code would be:
import os
import pandas as pd
data = [dict(name='file1',
filepath='Documents/file1.docx'),
dict(name='file2',
filepath='Documents/file2.docx')]
df = pd.DataFrame(data)
def make_clickable(url):
name= os.path.basename(url)
return '{}'.format(url,name)
df.style.format({'filepath': make_clickable})