How to open a pdf in a new tab in flask - python

Hey guys just doing a flask assignment for college to make a simple web application and im trying to open a pdf file in a new tab from my index page. I can open the file in the current tab no problem but cant wrap my head around opening it in a new tab. My code is attached below. Thanks for any time put into answering this.
I have tried using redirect and webbrowser.open_new_tab but might not have been using them correctly.
Dan
#app.get("/my_cv")
def my_cv():
workingdir = os.path.abspath(os.getcwd())
filepath = workingdir + '/static/files/'
return send_from_directory(filepath, 'DanielTurnerCV.pdf')

Related

When I run my python script the jpg file used in it becomes jpg.REMOVE_ME. What can I do?

I am programming a Instagram bot, which automatically posts images. When I run it the first time it works then it makes the .jpg file to a .jpg.REMOVE_ME file. What can I do?
This is my code:
from instabot import Bot
bot = Bot()
while True:
bot.login(username="username",
password="password")
bot.upload_photo("E:\lol.jpg",
caption="lol ")
You can simply comment line 190 and 191 in api_photo.py file
path: instabot\api\api_photo.py
The code which needs to be commented in line 190-191:
# if options.get("rename"):
# os.rename(photo, "{fname}.REMOVE_ME".format(fname=photo))
This is the expected behavior:
After successful upload, temporary photo will be renamed to {photo_name}.CONVERTED.jpg.REMOVE_ME in media folder
https://github.com/ohld/igbot/tree/master/examples/photos
BTW: Your code keep uploading the same picture.
If you want to upload another photo - update your code.
Do any of:
Rename it back.
Before the upload, copy it. Then upload the copy. Then remove the renamed copy.
Modify your local copy of the source code of instabot, and remove the codd which does the rename.

I'm using selenium to scrape a local web

I need to upload a file using 'upload' button. after that a window will appear but I can't find the exact ID from HTML code. here is the screen shots and my code:
`time.sleep(1)
element=driver.find_element_by_id("Upload-Action-Ico").click()
driver.find_element_by_xpath("//*[contains(text(), 'File')]").send_keys("file path")`
I think that the ID is 'file' so I think this should work
time.sleep(1)
element=driver.find_element_by_id("file").click()
Try click on it and show the HTML code. There is a word "Button" or similar. Can you share me the url of the site?
I hope I can help you and excuse me for my english. (It isn't my mother-language)
The input field does not contain any text. And its id is explicitly mentioned in the html, so you can try find_element_by_id:
driver.find_element_by_id("file").send_keys("file path")
If this doesn't work for you, then you can try using the xpath:
driver.find_element_by_xpath("//*[#id='file']").send_keys("file path")
You can use this code to select files, and after that, you should click on the upload button.
filePath = os.getcwd()+'\img.jpg'
driver.find_element_by_id('Upload-Action-Ico').send_keys(filePath)
os.getcwd() : returns the current working directory.
img.jpg is located right next to the running script in the same directory.

My python application creates an html file but when I freeze it with pyinstaller it does not create anything, why?

I've made a program that simply creates an HTML file in the location where the Python script is executed.
The problem is that after having freeze my application with PyInstaller, the executable no longer creates the HTML file as intended but does nothing. (It only makes the print statement of my program appear.)
Is there a way to get around this?
*I used the open("x.html","w+") function to create the HTML file.
The code that creates the HTML looks like this:
def create_html():
f = open("x.html", "w+")
f.write("<!DOCTYPE html>\n<html>\n<body>\n")
f.close()
I think what would fix the problem is to create the html file outside the working directory. However I have no idea how to do that.
After looking around I've found that my program does indeed work but creates the html file in the home directory on my mac. Is there a way to change that?
You can try
f = open ("x.html", "wb")
or if you need to add at the end of the file you can use
f = open ("x.html", "a")
although you can also use py2exe to generate an executable.
Are you sure your code is open("x.html", "w+") instead of open("/x.html", "w+")?
Try to use
open("./x.html", "w+") and try again.
You should learn about absolute path and relative path.
After looking around I've found that my program does indeed work but creates the html file in the home directory on my mac.
Is your program install in home directory?

Unable to open saved excel file using urllib.request.urlretrieve (Sample link mentioned )

Currently, I'm using Flask with Python 3.
For sample purposes, here is a dropbox link
In order to fetch the file and save it, I'm doing the following.
urllib.request.urlretrieve("https://www.dropbox.com/s/w1h6vw2st3wvtfb/Sample_List.xlsx?dl=0", "Sample_List.xlsx")
The file is saved successfully to my project's root directory, however there is a problem. When I try to open the file, I get this error.
What am I doing wrong over here?
Also, is there a way to get the file name and extension from the URL itself? Example, filename = Sample_List and extension = xlsx...something like this.

Python: How to open json and html files in notepad?

Im trying to open json and html files in notepad. I have the following code:
def openFile():
fileName = listbox_1.get(ACTIVE)
if fileName.endswith("json") or fileName.endswith("htm"):
os.system("notepad.exe" + fileName)
elif fileName.endswith("jpeg"):
os.startfile(fileName)
else:
messagebox.showerror(title="Error", message="File is not relevant, please choose one with a directory")
The problem is at the moment all that happens is that a command prompt window flashes up for about a second then disappears. I need for the content of the file to be displayed in notepad.
Using python 3.3 on windows
Thanks in advance
You've missed a space in system call. Proposed fix:
os.system('notepad.exe {}'.format(fileName))

Categories