Colour code the rows in CSV file in my react-django project - python

I have an application running with react as the front end and Django at the backend. I want to make a download button where my panda data frame can be exported to Excel/CSV file with colour coded format in the downloaded CSV.
Currently, i am using below code to download the file which is working but it gives me just the excel with data
import { CSVLink } from "react-csv";
<CSVLink
data={data}
filename={"my-file.csv"}
className="btn btn-primary"
target="_blank" >
Download me
</CSVLink>;

Related

django static file not loading after updating it on production

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>

how to write svg code to a file and save It in media folder in Django?

I have a function that generates a barcode as svg code.
I need to display this svg in an HTML PDF template. but unfortunately inline svg doesn't work.
so as a workaround I need to write this code into a file and save it in the media folder so I can access it by tag in the HTML PDF template.
Below is the function that generates the barcode:
def barcode_number(self):
number = self.get_real_instance().number
svg_code = generate('code39', str(number).zfill(20), pil=True)
code = svg_code.decode("utf-8")
## here I want to write code into barcode.svg file
safe_html = mark_safe(svg_code)
return safe_html
How can I write this code to a svg file and save it in '/media/uploads' ?
Thank you

How to download auto generated file in Django?

I am created a basic ocr system. When user uploads a PDF file to system ocr read it and created a xlsm file with the same name. I am using remote control with Azure (Windows). I created a download button. When I click it, downloads the file with same name, so it should work but appears a error.
How can I fix this problem?
<a href= "{% static path %}" class="btn btn-outline-primary btn-sm" download>Download Report File</a>
in my views:
name = pdf.pdf.name.replace(".xlsm", "").replace(".pdf", "")
path = "C:/user/ocr/" + name + ".xlsm"
Note: Uploaded and created files are stored outside of the project file.
please get more error info from django server. and list more code from my views.
Do you want to change file filename?

Download excel to open in browser instead of downloading directly? Python & Flask

Can I make an excel file open in the browser to be viewed instead of being downloaded, and have a download button, similar to how PDFs are?
I'm using Python Flask, for PDFs I do:
#blueprint_name.route("/download_some_pdf", methods=["GET"])
def download_pdf():
return send_file(file_path, cache_timeout=1)
This opens the PDF in a browser tab with the download PDF button
and for Excel files:
#blueprint_name.route("/download_some_xlsx", methods=["GET"])
def download_xlsx():
return send_from_directory(dir_path, filename, as_attachment=True, cache_timeout=1)
If for the Excel I remove the as_attachment parameter, or I use send_file instead of send_from_directory, it still downloads the file but with the name of the method ("download_xlsx") instead of the filename!!
I'm using Python 3.8.3 and Flask 1.1.2
So far as I know, whether to open the file depends on the client browser. I have encountered a browser who could not read the pdf and download it directly. So once you set the file to have the "GET" property it is upon the receiver to open it or simply save it in directory. Moreover, the "open pdf" only happens after the file is already fully downloaded in Temporary directory set by the browser.

Read new data from file and display it on html page

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

Categories

Resources