Python Flask : send and receive csv file - python

I have created a flask api and it is currently running in local machine
From one machine I ran the python script to post the text file:
import json
import requests
datas = {'var1' : 'var1','var2' : 'var2',}
#my file to be sent
local_file_to_send = 'C:\\Users\\tmp.txt'
with open(local_file_to_send, 'w') as f:
f.write('I am a file\n')
url = "http://127.0.0.1:5000/customerupdate"
files = [
('document', (local_file_to_send, open(local_file_to_send, 'rb'), 'application/octet')),
('datas', ('datas', json.dumps(datas), 'application/json')),
]
r = requests.post(url, files=files)
print("sd")
print(str(r.content, 'utf-8'))
Python flask API:
import json
from flask import Flask, request
from flask import send_file
app = Flask(__name__)
#app.route('/',methods=['GET'])
def hello_world():
return 'Hello World!'
#app.route('/customerupdate',methods=['GET','POST'])
def customerupdate():
posted_file = str(request.files['document'].read(), 'utf-8')
posted_data = json.load(request.files['datas'])
print(posted_file)
print(posted_data)
return '{}\n{}\n'.format(posted_file, posted_data)
if __name__=='__main__':
app.run(debug='true',threaded=True)
And the python get method to receive the file:
import requests
r = requests.get('http://127.0.0.1:5000')
print(r.text)
But here in get method I have received the contents of the file.But I want it for csv file.I tried by making that .csv instead of .txt.But can't able to send the file to api .So, Is it possible to send the csv file from one machine and receive the csv file in another machine via flask and please post the code if solutions available.
Already tried with this link:Python Flask: Send file and variable
I struggling lot to resolve it.

Related

How to upload file to minio from Flask-Restful without storing in disk

I want to store a file which is passed to Flask (Flask-Restful) from an API call to minio. However, I dont want the file to first be saved to my disk/memory before uploading it to minio. I tried what is mentioned in this question and while I didn't explicitly save the file to a directory, It was still uploaded to the disk. Is there any way to send the file to minio without having the entire content of the file on my disk/memory ?
from flask import Flask
from flask_restful import Resource, Api
from flask_restx import *
from requests import Request, Session
app = Flask(__name__)
api = Api(app)
base_parser = reqparse.RequestParser()
parser_post = base_parser.copy()
class PostFileEndpoint(Resource):
def post(self):
file_data = parser_post.parse_args().get('file_data')
if check_valid_request(file_data):
# Upload to minio
with Session() as s:
req = Request(
method="PUT",
url=minio_url,
data=file_data
)
resp = s.send(req.prepare(), stream=True)
return {'task': 'File Uploaded'}, 200
else:
return {'task': 'Bad Request'}, 400
api.add_resource(PostFileEndpoint, '/')
if __name__ == '__main__':
app.run(debug=True)
Clarifying, I understand that it may not be possible to upload the file to minio without storing some part of it in memory/disk of the server where the flask app is running, but I wanted to know if it is possible for me to upload the file without having the entire content of the file in the disk/memory at the same time.

Read audio file in a POST request to Flask API

I am building a "guitar tuner" app with ReactJS for frontend and Python Flask as the backend.
This is what the app does so far:
1. The React app (client side) records audio using react library react-mic
2. Sends the recording via. a fetch POST request to to Flask API, which picks it up and send as response back.
PROBLEM: The file being sent is on the form in the screenshot, which is a list with one Blob element consisting of a webm audio file.
When I send this blob file of a webM audio file in the fetch function it comes out as undefined in the Flask app, and I am unsure about how to read the blob/webm audio in Python.
The POST function in ReactJS:
uploadFile(file) {
var form = new FormData();
form.append('file',file)
form.append('title',"Guitar recording")
fetch('http://127.0.0.1:5000/audio_record', {
// content-type header should not be specified!
method: 'POST',
body: form
}).then(function (response){
return (response.text())
}).then(function(text){
console.log(text) // The text the endpoint returns
})
.catch(error => console.log(error)
);
}
Python flask app (where I try reading the file, does not work..):
import audioread
from flask import Flask, request #import main Flask class and request object
from flask_cors import CORS
import logging
from pydub import AudioSegment
from pydub.playback import play
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('HELLO WORLD')
app = Flask(__name__) #create the Flask app
CORS(app)
#app.route('/')
def landing():
return 'Landing page'
# Get the blob of type "audio/webm;codecs=opus"
#app.route('/audio_record', methods=['POST'])
def save_record():
logger.info("welcome to upload`")
# file = request.files['file']
#filename = secure_filename(file.title)
file = request.form['file']
print('File from the POST request is: {}'.format(file))
try:
read_audio_file(file[0])
return "****** Audio Read ******"
except:
print("In the except", file[0]) # Gets printed as undefined
title = request.form['title']
print(title) # Able to print title
return "Request received and responded"
# app.logger.debug(request.files['file'].filename)
def read_audio_file(audio_from_post):
print("Tring to read audio..")
with audioread.audio_open(audio_from_post) as f:
print(f.channels, f.samplerate, f.duration)
for buf in f:
print(buf)
if __name__ == '__main__':
app.run(debug=True, port=5000) #run app in debug mode on port 5000
I saw here that it might be smart to convert the Blob to an Audio object in ReactJS, but I am not sure how that would make the reading of the file in Flask any easier.
Any idea as to how I should do this?
I want to read the file in Python and do a Fast Fourier Transform (numpy.fft.fft), to determine the frequencies in the audio clip.
Thanks in advance!
UPDATE
I decided that I wanted to try recording the audio with another library, MediaRecorder- to be able to record audio in WAV, not webm. I think I will encode the WAV file in base64, send it in a form to Flask and read it with the wave library.
One way you could approach is by sending the data from the frontend is sending the blob of the recording instead of the whole object by making the following change to your POST function in React
form.append('file',file.blob)
Then, on the server-side, you could simply do the following
request.files['file'].filename
And then proceeding to save it by using the built-in save method for further processing. Documentation for request.files can be found here

Creating a minimal flask/request file uploader [duplicate]

This question already has answers here:
How to upload file with python requests?
(9 answers)
Closed 4 years ago.
I was trying to create a minimal flask server/client python movie file uploader but my client-code doesn't seem to be working properly and I'm wondering if I need more then what I have?
Server.py
from flask import Flask, request
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST', 'PUT'])
def hello_world():
file = request.files
return(str(file))
running as: flask run
Uploader.py
import requests
files = {'file': open("BigBuckBunny_320x180.mp4")}
r = requests.post("http://127.0.0.1:5000/", files)
print(r.text)
running as: python Uploader.py
However the hello_world method returns ImmutableMultiDict([])
For Debugging purposes I've used this following curl snippet which seems to work:
curl -i -X PUT -F filedata=#BigBuckBunny_320x180.mp4 "http://localhost:5000/"
and returns
ImmutableMultiDict([('file', <FileStorage: u'BigBuckBunny_320x180.mp4' ('application/octet-stream')>)])
Any Idea why the Uploader.py fails?
I tried example you gave us and I think I managed to find a solution.
After some digging, I found that you can stream requests with requests module:
Requests supports streaming uploads, which allow you to send large streams or files without reading them into memory.
You just need to provide a file to be streamed and open it in read binary mode, rb.
app.py
from flask import Flask, request
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST', 'PUT'])
def hello_world():
# 'bw' is write binary mode
with open("BigBuckBunny_320x180_flask_upload.mp4", "bw") as f:
chunk_size = 4096
while True:
chunk = request.stream.read(chunk_size)
if len(chunk) == 0:
return 'Done'
f.write(chunk)
if __name__ == '__main__':
app.run()
Uploader.py
import requests
# Give proper path to your file, I used mine from flask app directory
with open('BigBuckBunny_320x180.mp4', 'rb') as f:
requests.post('http://127.0.0.1:5000/', data=f)
Check this article.

Send and receive data from local client machine to a flask server on an Ubuntu server

Can someone please help me with my query.
On my local machine, I have a server and client implemented in Python. The JSON data is sent from client to server, the server parses the required data out of it and sends the result as JSON file back to the client. This is running fine on my local machine. I now want to implement this flask server on an Ubuntu server and then want to send and receive data. I am trying to use mod_wsgi as explained in
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
I am still not able to get the data back to my local machine. Here is my code:
client.py
import sys
import json
import requests
import time
import os
import glob
data_location = 'C:\\Users\\cathy\\Desktop\\data' # folder containing all the data
for root, directories, files in os.walk(data_location):
for directory in directories:
loc = (data_location + '/' + directory + '/*')
all_files = glob.glob(loc)
for filename in all_files:
f=open(filename)
f=f.read().splitlines()
payload = {'input': f}
s = json.dumps(payload)
#res = requests.post("http://127.0.0.1:5000/my_data/", json=s).json()
res = requests.post("http://12.345.678.890/my_data/", json=s).json()
#time.sleep(10)
if res['employee_id']:
print(res['employee_id'])
if res['name']:
print(res['name'])
server.py
from flask import Flask
from flask import request
import json
import re
import sys
import os
import time
from parsers import id_parser, name_parser
import spacy
import re
from datetime import datetime#
nlp = spacy.load('en_core_web_lg')
import glob
app = Flask(__name__)
#app.route('/my_data/', methods = ['POST'])
def parsing_data():
jsondata = request.get_json()
data = json.loads(jsondata)
requiredData=data['input']
employee_id_=id_parser(requiredData)
name=name_parser(requiredData)
result = {'employee_id_': employee_id_, 'name':name}
return json.dumps(result)
if __name__ == '__main__':
app.run(debug=True)
Instead of json.dump() try using flask jsonify method that will be compatible within flask.
#......
from flask import jsonify
#......
#app.route('/my_data/', methods = ['POST'])
def parsing_data():
#.....
result = {'employee_id_': employee_id_, 'name':name}
return jsonify(result)
I'm not shure if it will help, but try it. :)

Cannot open a file with json in OpenShift with python

I have a problem with python code in Openshift.
I have a subdomain on my app where I have to open a txt file with json format.
from flask import Flask
from flask import render_template
import json
app = Flask(__name__)
#app.route("/")
def index():
return render_template("home.html")
#app.route('/casestudy1')
def cs1():
json_data = open("cs1.txt")
data = json.load(json_data)
....do my staff....
return render_template("cs1.html")
if I remove the first two lines from cs1() the app works perfect. I tried to run flask localy from command line and it works there as well. The cs1.txt file is in the same root with the main.py.
Error: Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Error on log:
json_data = open("cs1.txt")
IOERROR: [Errno2] No such file or directory: cs1.txt
Application root:
-wsgi
-static
-css
-js
-templates
-cs1.html
-main.py
-cs1.txt
I found the problem.
import os
json_data = open(os.path.join(os.path.dirname(__file__),"cs1.txt"),'r')
instead of
json_data = open("cs1.txt")

Categories

Resources