On my Raspberry Pi I've built a script that reads data from a file and shows a live video stream.
The content is then displayed on an html page:
f = open("demofile.txt", "r")
temperature=f.read()
f.close()
print(temperature)
PAGE="""\
<html>
<body>
<center><img src="stream.mjpeg" width="640" height="480"></center>
<center><h2>%s</h2></center>
</body>
</html>
""" %(temperature)
My problem is, that the file is being updated every 10 minutes - how can I reload or pull the new content from the file each time I access the html page?
Since I'm streaming video, the script runs all the time so I can't stop it and run it again to get the most updated data from the file.
Please advise
Related
On a django web app. I have a script which runs when i goes to myapp.com/update url it fetches data using bs4 library and convert the data into a pandas dataframe.
To show that data I used pd.to_html to convert it in HTML table format and stores that HTML file in static folder and then load it in my index.html(which present in templates folder using jquery).
The HTML loads fine but after the I visit the update url it stops showing the updated HTML file (pandas df one). The updated HTML file (pandas df one) is still available on azure panel. The site is not loading it anymore but before update it was loading perfectly.
How do I display the updated HTML file?
--script in my views.py (for myapp.com/update url)--
text_file2 = open("staticfiles/album.html", "w")
text_file2.write(updatedData)
text_file2.close()
This is how I'm adding that HTML file to my main index.html file
(this loads the file perfectly but after update url it stops loading it)
<script>
$(function () {
$("#includedAlbums").load("../static/album.html");
});
</script>
I am writing a python script that generates a html file out of some images and svg files, which later should be converted into a pdf file.
For the conversion I am using weasyprint, but for some reason it doesnt render the images, only the according alt-text.
Here is a screenshot of the html file rendered in Chrome:
But the generated pdf looks like this:
Here is the code that should generate the pdf:
html = HTML(filename="path/to/file")
html.write_pdf("path/to/output/file")
And here is the according html file:
<img src="C:/Users/jonas/Documents/Dev/PythonLabel/bin/LABEL_SVG.svg" alt="SVG of Label" />
<div class="sn-number">3253343345</div>
<img class="qr-code" src="C:/Users/jonas/Documents/Dev/PythonLabel/bin/qrcode.png" alt="qrcode" />
I dont know what causes the problem, but Firefox isnt able to render the images either, but Chrome and Brave manage to do so. Maybe both problems have a similar reason.
So I managed to get it to work, but its not a pretty solution.
I figured out, that weasyprint was able to get images and SVGs from actual webpages like stackoverflow. So I made a little flask app that would only serve the one html file, and after the PDF was created I just stopped the flask server.
from flask import Flask
from multiprocessing import Process
app = Flask(
__name__, static_folder=path/to/static/folder)
#app.route("/")
def home():
with open(path/to/html, "r") as f:
html = "".join(f.readlines())
return html
def start_server():
app.run()
if __name__ == "__main__":
server = Process(target=start_server)
server.start()
sleep(4)
print("Generating PDF...")
html = HTML(
"http://localhost:5000").write_pdf(path/to/output/file)
server.terminate()
server.join()
I am working on a script to extract text from law cases using https://case.law/docs/site_features/api. I have created methods for search and create-xlsx, which work well, but I am struggling with the method to open an online pdf link, write (wb) in a temp file, read and extract the data (core text), then close it. The ultimate goal is to use the content of these cases for NLP.
I have prepared a function (see below) to download the file:
def download_file(file_id):
http = urllib3.PoolManager()
folder_path = "path_to_my_desktop"
file_download = "https://cite.case.law/xxxxxx.pdf"
file_content = http.request('GET', file_download)
file_local = open( folder_path + file_id + '.pdf', 'wb' )
file_local.write(file_content.read())
file_content.close()
file_local.close()
The script works well as it download the file and it created on my desktop, but, when I try to open manually the file on the desktop I have this message from acrobat reader:
Adobe Acrobat Reader could not open 'file_id.pdf' because it is either not a supported file type or because the file has been damager (for example, it was sent as a email attachments and wasn't correctly decoded
I thought it was the Library so I tried with Requests / xlswriter / urllib3... (example below - I also tried to read it from the script to see whether it was Adobe that was the issue, but apparently not)
# Download the pdf from the search results
URL = "https://cite.case.law/xxxxxx.pdf"
r = requests.get(URL, stream=True)
with open('path_to_desktop + pdf_name + .pdf', 'w') as f:
f.write(r.text)
# open the downloaded file and remove '<[^<]+?>' for easier reading
with open('C:/Users/amallet/Desktop/r.pdf', 'r') as ff:
data_read = ff.read()
stripped = re.sub('<[^<]+?>', '', data_read)
print(stripped)
the output is:
document.getElementById('next').value = document.location.toString();
document.getElementById('not-a-bot-form').submit();
with 'wb'and 'rb' instead (and removing the *** stripped *** the sript is:
r = requests.get(test_case_pdf, stream=True)
with open('C:/Users/amallet/Desktop/r.pdf', 'wb') as f:
f.write(r.content)
with open('C:/Users/amallet/Desktop/r.pdf', 'rb') as ff:
data_read = ff.read()
print(data_read)
and the output is :
<html>
<head>
<noscript>
<meta http-equiv="Refresh" content="0;URL=?no_js=1&next=/pdf/7840543/In%20re%20the%20Extradition%20of%20Garcia,%20890%20F.%20Supp.%20914%
20(1994).pdf" />
</noscript>
</head>
<body>
<form method="post" id="not-a-bot-form">
<input type="hidden" name="csrfmiddlewaretoken" value="5awGW0F4A1b7Y6bx
rYBaA6GIvqx4Tf6DnK0qEMLVoJBLoA3ZqOrpMZdUXDQ7ehOz">
<input type="hidden" name="not_a_bot" value="yes">
<input type="hidden" name="next" value="/pdf/7840543/In%20re%20
the%20Extradition%20of%20Garcia,%20890%20F.%20Supp.%20914%20(1994).pdf" id="next">
</form>
<script>
document.getElementById(\'next\').value = document.loc
ation.toString();
document.getElementById(\'not-a-bot-form\').submit();
</script>
<a href="?no_js=1&next=/pdf/7840543/In%20re%20the%20Extradition%20of%20Garcia,%2
0890%20F.%20Supp.%20914%20(1994).pdf">Click here to continue</a>
</body>
</html>
but none are working. The pdf is not protected by a password, and I tried on other website and it doesn't work either.
Therefore, I am wondering whether I have another issue that is not link to the code itself.
Please let me know if you need additional information.
thank you
It looks like instead of the PDF the web server is providing you with a web page intended to prevent bots from downloading data from the site.
There is nothing wrong with your code, but if you still want to do this you'll have to work around the bot prevention of the website.
I am reading user inputs and sending them for processing. After processing, the results are displayed. Along with the results I want a link on webpage to be able to download the data as a csv file. My function to process inputs looks as follows.
#app.route('/process', methods=['POST'])
def process_data():
# create csv_file
return render_template("results.html", data=csv_file)
results.html has following line.
<p> <large>Download simulation data </large></p>
This link is correctly displayed on the webpage. My function to download the data looks as follows.
#app.route('/download/<filename>')
def download(filename):
response = make_response(filename)
response.headers["Content-Disposition"] = "attachment; filename=Simulation.csv"
response.headers["Content-Type"] = "text/csv"
return response
Clicking the download link, I get '414 Request-URI Too Large'.
Is there a better solution for passing data from flask to html to flask again?
I can see that my entire data are appended to url, can I somehow avoid that?
Is it possible to directly pass response while rendering results.html and make it downloadable?
UPDATE
I learnt that putting data in url is a bad idea. Instead I can use dataurl by encoding the data and then use dataurl for csv in href tag of html.
buffer = StringIO()
dataframe.to_csv(buffer, index=False)
buffer.seek(0)
data_str = base64.b64encode(buffer.getvalue().encode('utf8')).decode('ascii')
url = "data:text/csv; base64,{}".format(data_str)
html looks like as follows.
<a download="SSW_Simulation.csv" href="{{ data_url }}">download</a>
However this solution does not work in internet explorer I guess because urls for data are not supported. Should I be saving the csv file somewhere and pass that filename to html? so that when prompted, I can fetch it from the temporary location and download using make_response? I would prefer not to save the file to the disk.
Handling the data in javascript solved the problem as suggested by #Jeronimo. json string was passed to html page.
import json
#app.route('/process', methods=['POST'])
def process_data():
# create csv_file
data = json.dumps(csv_file)
return render_template("results.html", data=data)
Javascript suggested in this answer was added to html and along with download button.
<button onclick="download({{ data }}, 'myfile.csv', 'text/csv')">download data</button>
I have a python cgi script that creates a text area and fills it with default value from the contents of a file. This used to work but recently I noticed that with change in content on the file ;the html is rendered incorrectly and the submit button and some parts of the file contents to be shown in the text area(as default content) etc is missing or messing up with the total page's html
print('<form action="x.cgi" method="post">')
print('<textarea name="textcontent" cols="120" rows="50">')
with open('somefile', 'r') as content_file:
content = content_file.read()
content_file.close()
print(content)
print('</textarea>')
print('<HR>')
print('<input type="submit" value="Submit" />')
print('</form>')
What can be done so that the contents of somefile doesnt mess with the html form . Note that somefile is a configuration file and I need everything in the file to be printed as such so user can make necessary change and submit it