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>
Related
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>;
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
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?
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 saved an HTML as a static file in order to save it in cache and render it through a service worker when Offline. I would like to make a small change on my HTML in order to get the appropriate encoding. I am using pythonanywhere as my server. And I have defined in my settings.py the following paths:
STATIC_URL = '/static/'
STATIC_ROOT = u'/home/cocoa/cocoa/static'
When I look at my file in the server in the following directory is:
/home/cocoa/cocoa/static/login/Offline/HTML/mEstado.html
My file looks like this:
If I visit the following URL:
https://cocoa.pythonanywhere.com/static/login/Offline/HTML/mEstado.html
My file looks like this:
I allready tried reloading on shift. Unabling the service-worker and
python manage.py collectstatic
But nothing seems to work. I do not know where the problem is, nor how to solve it. Any help would be appreciated.
Did you try to clear you browser cache? Ctrl+F5. I think problem in cached html file because you run it from static. As I know browser caches all static files from django static folder.