« first day (3671 days earlier)      last day (1278 days later) » 
00:00 - 20:0020:00 - 22:00

8:11 PM
queston about flask send_file, send_from directory
I have a file stored as a variable, is it posible to send the file via these functions, or does this files require a saved file ?
 
@Amundsen How can it be a file if it isn't saved? What type of object contains the content you want to send?
 
a pandas df
 
@BožoStojković ooops... tyvm
 
send_file will take an open "file-like object" as its first argument, so you can send the contents of (e.g.) an io.BytesIO object. send_from_directory can't work this trick as it requires a directory path and a name.
I'm wondering what kind of clients will accept a pandas dataframe - does it have a mimetype?
 
@holdenweb not that I'm aware of... you'd normally send it as a json representation (or try to as it can have datatypes that can't really be json encoded)
 
8:18 PM
But you'll then need to find a way to turn a pandas df into a bytestream to write it to the BytesIO.
 
you don't normally try to transport a pandas DF though
 
@Amundsen What do you want to send it as? A CSV, for example?
 
all depends what you actually want to do with it and what resources are available and how it is
 
jeah sorry, forgot to mention this, as a xlsx
 
More specifically, is this something like a user clicks "download file" and it downloads via the browser. Am I correct?
 
8:20 PM
yes
 
Ok, un momento por favor
I need to shut down the project I've been staring at for ages anyway, so a good excuse to fire up another project :)
 
@Amundsen Unfortunately pandas.pydata.org/pandas-docs/stable/reference/api/… seems to imply that pandas.ExcelWriter must send its output to filestore.
 
@holdenweb nah... I've got systems running that send emails and attach Excel reports just fine
 
In which case you'll need to create a unique temporary file then send that with either of the functions you began this thread with. Why the resistance to writing a file?
@JonClements You've used pandas.ExcelWriter to write to a BytesIO?
 
8:26 PM
@holdenweb yup
 
because the app does not need to store files, thought would be much cleaner
 
@JonClements How? Its first argument is a path!
 
@Amundsen I thought I sent Excel files somewhere but the examples I can find are all CSVs
@bp.route('/forecasts/export_annual_forecast', methods=['POST'])
@login_required
def export_annual_forecast():

    forecast = AnnualForecast.get_forecast(2019)[0] # This is a df
    headers = list(forecast.columns)
    si = io.StringIO()
    cw = csv.writer(si)
    cw.writerow(headers)
    cw.writerows(forecast.values.tolist())

    output = make_response(si.getvalue())
    output.headers["Content-Disposition"] = ('''attachment; '''
                                             '''filename=forecast.csv''')
 
@holdenweb one mo'... will dig up example code
 
@AndrasDeak Related but distinct from that question
 
8:29 PM
OK, thanks
 
Also, thanks for the hammer earlier, just realised I didn't thank you for helping out :)
 
too late, I'm already hurt
 
i think i found how to write to io object
34
A: Write to StringIO object using Pandas Excelwriter?

jmcnamaraPandas expects a filename path to the ExcelWriter constructors although each of the writer engines support StringIO. Perhaps that should be raised as a bug/feature request in Pandas. In the meantime here is a workaround example using the Pandas xlsxwriter engine: import pandas as pd import Stri...

so i need to pass the io object to the send_file method to send the data ?
 
I'm not sure that's the right method to be using in Flask. I need to check the source
In the back on my mind, that's going to want a file path too
 
okay, found the crontab script:
TODAY="$(date +%Y%m%d)"
for spider in $(venv/bin/scrapy list); do
    #echo $TODAY $spider
    venv/bin/scrapy crawl $spider -L INFO -s LOG_FILE="output/logs/${TODAY}_$spider.log" -o "output/raw/${TODAY}_$spider.csv"
done
venv/bin/python generate_report.py $TODAY
 
8:39 PM
@Amundsen yeah I think that's going to complain
 
In case @Pijes returns, here is the math to fit the quadratic expression to those points: mth.bz/fit/polyfit/findcurv.htm#02
 
found this on the internetz, dude had same question
 
ugh... that one never returns the actual content as a file type (just stores in a web directory)
 
thats going to be it :)
1
A: Alternative of send_file() in flask on Pythonanywhere?

RutrusFrom the mentioned forum in Mat's answer I verified that: [send_file()] does not work because the uWSGI file wrapper does not support file-like objects, only real files ...but applying Mat's solution still throws me a ValueError: I/O operation on closed file. Even with FileWrapper class. This w...

 
8:46 PM
Thank you for reminding me that I still don't like dealing with data.
 
Waaait a minute. Am I being stupid here? This references V2.0 but there is no V2.0 for Flask?
 
That's a fancy way of saying # FIXME
 
2.0 is development version
> Warning: This is the development version. The latest stable version is Version 1.1.x.
 
In which case, it looks like it won't be broken (yet) @Amundsen
Is there anything more to that system than just being "fancy"? I'm guessing with it being davidism that it allows certain test suites to run differently. Otherwise why not V1.2?
Well, I guess I can answer that myself. 2.0 is a global container for lots of implementations and then you just choose which ones to pull in to the 1.2 release
I think?
 
9:01 PM
My guess is that the change could be a breaking one, hence 2.0? If that's how they do versioning, anyways
 
Probably would be a breaking change but it's implemented on the dev branch, so presumably muddled in with every other change after 1.1. I guess it's a case of cherry-picking when it comes to an actual release version
or there isn't going to be a 1.2 <shrug/>
 
00:00 - 20:0020:00 - 22:00

« first day (3671 days earlier)      last day (1278 days later) »