I have this code in Node.js
let options = req.body
PythonShell.run('./chat.py', options, function (err, results) {
console.log(results)
})
In my chat.py file, I have this:
import sys
import json
def chatFun():
options = json.loads(sys.argv[1])
print(options)
print(sys.argv[1])
return
chatFun()
When I run my code without the print(sys.argv[1]), and just put print("hello world"), it works, but then I put print(sys.argv[1]) and it gives me:
null
null
Not sure why this is happening. Could anyone share some advice.
value of options:
city: New York,
party_size: 5,
stay: hotel
I've read through numerous answers on SO, but most don't show both the Flask and JS side of my question, so I don't believe this is a duplicate. Specifically, using React/JS, I'm attempting to fetch a list of dictionaries from my Flask API.
On the server side, I've successfully serialized my class, and can print the list of dictionaries as expected. However, after I return it using JSONIFY (as suggested in several SO questions), it results in undefined on the JS side when I try to access the data. From reading online, it appeared that I could attempt to JSON parse the response data, but this similarly shows undefined. This leads me to believe that the issue may in fact be on the server side. Regardless, I'm also unsure how to properly access this list of dictionaries in my fetch function (I would've expected that I can do something along the lines of data[0]['name']).
I'd really appreciate any guidance you might have - thank you for reading this far.
eqtls prints as follows in api.py:
[{'id': 1, 'name': 'Bearbrook Skateboard Park', 'description': 'Flat asphalt surface, 5 components', 'xcoord': -75, 'ycoord': 45, 'date_added': datetime.datetime(2021, 10, 26, 19, 46, 10)}]
api.py
class Activity(db.Model):
id = db.Column(db.Integer, primary_key=True)
xcoord = db.Column(db.Integer,nullable=False)
ycoord = db.Column(db.Integer, nullable=False)
name = db.Column(db.String(200))
description = db.Column(db.String(200))
date_added = db.Column(db.DateTime, default = datetime.utcnow)
#create a string
def __repr__(self):
return f"<id={self.id}, name={self.name},description={self.description},xcoord={self.xcoord},ycoord={self.ycoord},date_added={self.date_added}>"
def serialize(self):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'xcoord': self.xcoord,
'ycoord': self.ycoord,
'date_added': self.date_added
}
#app.route('/allActivities', methods=['GET'])
def allActivities():
print('hit allActivities routing')
allActivities = Activity.query.all()
print(str(allActivities))
eqtls=[activity.serialize() for activity in allActivities]
print(str(eqtls))
sys.stdout.flush()
return jsonify(eqtls)
Currently in app.js, my data variable logs as undefined.
app.js
useEffect(()=> {
fetch('/allActivities').then(response => {
if(response.ok){
console.log(response)
return response.json()
}
}).then(data => setAllActivities(data))
.then(data => {
console.log(data);
//var jsonData = JSON.parse((data));
//console.log(jsonData[0].name);
})
},[])
I m trying to get an array from react frontend (stored in local storage) to my view class in django but i'm getting this error:
In console:
GET http://127.0.0.1:8000/api/quiz/multiple/ 500 (Internal Server Error)
Django LOGS:
for quiz in quizzes:
TypeError: 'NoneType' object is not iterable
ERROR:django.server:"GET /api/quiz/multiple/ HTTP/1.1" 500 20064
Here's how i store the data in the LocalStorage:
localStorage.setItem('quizzes', JSON.stringify(quizList));
history.push('/start')
And here's how i get it from local storage and pass it to the django using axios:
export default function QuizPage() {
const [DataState,setDataState] = useState([]);
const storedQuizzes = JSON.parse(localStorage.getItem("quizzes"))
useEffect(() => {
axiosInstance
.get(`quiz/multiple/`, {
quizzes: storedQuizzes
}).then((res) => {
setDataState(res.data);
})
.catch((function (error) {
console.log(error)
}));
}, [setDataState]);
and, finally, that's my django view:
class MultipleQuizView(APIView):
permission_classes = [IsAuthenticated]
def get(self,request):
questionsList = []
quizzes = request.data.get('quizzes')
for quiz in quizzes:
currentQuiz = Quiz.objects.get(url=quiz)
quizSerializer = QuizSerializerForMultipleQuizzes(currentQuiz)
question = Question.objects.filter(quiz__url=quiz)
questionSerializer = QuestionSerializer(question, many=True)
quizSerializerData = quizSerializer.data.copy()
quizSerializerData["questions"]=questionSerializer.data
questionsList.append(quizSerializerData)
if questionsList:
return Response(questionsList)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
I'm pretty sure the problem isn't from my view class because i tested it using Postman and it works without any problem.
EDIT:
I just tryed with postman using this body and it works properly:
https://i.stack.imgur.com/3RJ5A.png
So i need to send data from react like this but i don't know how:
{
"quizzes":["securitate","aparare"]
}
Try changing the second param to axios.get as follows:
axiosInstance
.get(`quiz/multiple/`, {
params: {
quizzes: storedQuizzes
}
}).then(...)
Read more about the properties that the second param supports.
SOLVED!
The problem was that i wrote:
quizzes = request.data('quizzes')
instead of:
quizzes = request.data['quizzes']
I'm trying to send POST requests from a NEXTjs frontend with a simple form field to a backend located on the same server which is a python script using the Falcon library. The python script itself is ran by Gunicorn and listens on the 8080 port.
Both codes run pretty well without errors but when I try to submit the form all I get is a 415 error which seems to indicate that what I'm trying to send to the API is not a supported media type but, as pointed out in this answer
Falcon has out of the box support for requests with Content-Type: application/json
Since the webpage and the server are hosted on the same VPS I've also tried to use the 127.0.0.1 address in the fetch call but that was unsuccessful as well (the backend API didn't even responded in fact)
Here's the backend code:
#!/usr/bin/env python
# coding=utf-8
import time
import falcon
import json
class Resource(object):
def on_post(self, req, resp, **kwargs):
request_body = req.media
print('POST Request: {}'.format(req))
print('Request body: {}'.format(request_body))
start = time.time()
resp.body = json.dumps({
'count_identical_pairs': count_identical_pairs(request_body),
'computation_time': int((time.time() - start) * 1000)
})
def count_identical_pairs(integers_array):
total = 0
count = dict()
# Type checking
if not isinstance(integers_array, list):
return -1
# Check if N is within the range [0..100,000]
if len(integers_array) > 100000:
return -2
for integer in integers_array:
# Check if each element of the array is within the range [−1,000,000,000..1,000,000,000]
if integer not in range(-1000000000, 1000000000):
return -3
if str(integer) not in count:
count[str(integer)] = 1
else:
count[str(integer)] += 1
for key, value in count.items():
total += value * (value - 1) / 2
return total
api = application = falcon.API()
api.add_route('/count_identical_pairs', Resource())
And here's the frontend one:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class Index extends React.Component {
constructor() {
super();
this.state = {
input_array: [],
};
this.onSubmit = this.onSubmit.bind(this);
this.myHeaders = new Headers();
}
onChange = evt => {
// This triggers everytime the input is changed
this.setState({
[evt.target.name]: evt.target.value,
});
};
onSubmit = evt => {
evt.preventDefault();
console.log('this.state.input_array = ' + this.state.input_array);
console.log('JSON.stringify(this.state.input_array) = ' + JSON.stringify(this.state.input_array));
// Making a post request with the fetch API
// Test payload [1, 7, 7, 5, 7, 5, 6, 1]
fetch('http://vps638342.ovh.net:8080/count_identical_pairs', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8'
},
mode: 'no-cors', // Security hazard?
body: JSON.stringify(this.state.input_array),
redirect: 'follow'
})
.then(response => response.text())
.then(data => console.log('Data: ' + data))
.catch(error => console.log('Error: ' + error))
};
render() {
return (
<form onSubmit={this.onSubmit} >
<input
name="input_array"
type="text"
id="name"
value={this.state.input_array}
onChange={this.onChange}>
</input>
<input type="submit" />
</form>
);
};
}
ReactDOM.render(<Index />, document.getElementById("root"));
EDIT 1: I've tested the python backend API with Postman and I can see that it works pretty well already as you can see pictured here:
EDIT 2: Thanks to #Maku here's the update code on the backend that allows all origins, methods and header. I'm new to server development but I'm guessing it's not a very secure way to code but at least it works (I'll add a third EDIT if I find a more recommended way to do this)
Enable CORS in your falcon server and remove the 'no-cors' flag in your javascript, that worked for me the other day.
https://github.com/lwcolton/falcon-cors should work for you. To test it you could just allow all origins with something like this (I'm using another python framework so I haven't tested this exact falcon extension)
cors = CORS(allow_all_origins=True, allow_all_headers=True)
api = falcon.API(middleware=[cors.middleware])
Edit: added allow_all_headers=True like discussed in the comments.
I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I return a JSON response?
#app.route("/summary")
def summary():
d = make_summary()
# send it back as json
A view can directly return a Python dict or list and Flask will call jsonify automatically.
#app.route("/summary")
def summary():
d = make_summary()
return d
For older Flask versions, or to return a different JSON-serializable object, import and use jsonify.
from flask import jsonify
#app.route("/summary")
def summary():
d = make_summary()
return jsonify(d)
jsonify serializes the data you pass it to JSON. If you want to serialize the data yourself, do what jsonify does by building a response with status=200 and mimetype='application/json'.
from flask import json
#app.route('/summary')
def summary():
data = make_summary()
response = app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
return response
Pass keyword arguments to flask.jsonify and they will be output as a JSON object.
#app.route('/_get_current_user')
def get_current_user():
return jsonify(
username=g.user.username,
email=g.user.email,
id=g.user.id
)
{
"username": "admin",
"email": "admin#localhost",
"id": 42
}
If you already have a dict, you can pass it directly as jsonify(d).
If you don't want to use jsonify for some reason, you can do what it does manually. Call flask.json.dumps to create JSON data, then return a response with the application/json content type.
from flask import json
#app.route('/summary')
def summary():
data = make_summary()
response = app.response_class(
response=json.dumps(data),
mimetype='application/json'
)
return response
flask.json is distinct from the built-in json module. It will use the faster simplejson module if available, and enables various integrations with your Flask app.
To return a JSON response and set a status code you can use make_response:
from flask import jsonify, make_response
#app.route('/summary')
def summary():
d = make_summary()
return make_response(jsonify(d), 200)
Inspiration taken from this comment in the Flask issue tracker.
As of version 1.1.0 Flask, if a view returns a dict it will be turned into a JSON response.
#app.route("/users", methods=['GET'])
def get_user():
return {
"user": "John Doe",
}
If you want to analyze a file uploaded by the user, the Flask quickstart shows how to get files from users and access them. Get the file from request.files and pass it to the summary function.
from flask import request, jsonify
from werkzeug import secure_filename
#app.route('/summary', methods=['GET', 'POST'])
def summary():
if request.method == 'POST':
csv = request.files['data']
return jsonify(
summary=make_summary(csv),
csv_name=secure_filename(csv.filename)
)
return render_template('submit_data.html')
Replace the 'data' key for request.files with the name of the file input in your HTML form.
Flask 1.1.x supports returning a JSON dict without calling jsonify. If you want to return something besides a dict, you still need to call jsonify.
#app.route("/")
def index():
return {
"api_stuff": "values",
}
is equivalent to
#app.route("/")
def index():
return jsonify({
"api_stuff": "values",
})
See the pull request that added this: https://github.com/pallets/flask/pull/3111
I use a decorator to return the result of jsonfiy. I think it is more readable when a view has multiple returns. This does not support returning a tuple like content, status, but I handle returning error statuses with app.errorhandler instead.
import functools
from flask import jsonify
def return_json(f):
#functools.wraps(f)
def inner(**kwargs):
return jsonify(f(**kwargs))
return inner
#app.route('/test/<arg>')
#return_json
def test(arg):
if arg == 'list':
return [1, 2, 3]
elif arg == 'dict':
return {'a': 1, 'b': 2}
elif arg == 'bool':
return True
return 'none of them'
Prior to Flask 0.11, jsonfiy would not allow returning an array directly. Instead, pass the list as a keyword argument.
#app.route('/get_records')
def get_records():
results = [
{
"rec_create_date": "12 Jun 2016",
"rec_dietary_info": "nothing",
"rec_dob": "01 Apr 1988",
"rec_first_name": "New",
"rec_last_name": "Guy",
},
{
"rec_create_date": "1 Apr 2016",
"rec_dietary_info": "Nut allergy",
"rec_dob": "01 Feb 1988",
"rec_first_name": "Old",
"rec_last_name": "Guy",
},
]
return jsonify(results=list)
In Flask 1.1, if you return a dictionary and it will automatically be converted into JSON. So if make_summary() returns a dictionary, you can
from flask import Flask
app = Flask(__name__)
#app.route('/summary')
def summary():
d = make_summary()
return d
The SO that asks about including the status code was closed as a duplicate to this one. So to also answer that question, you can include the status code by returning a tuple of the form (dict, int). The dict is converted to JSON and the int will be the HTTP Status Code. Without any input, the Status is the default 200. So in the above example the code would be 200. In the example below it is changed to 201.
from flask import Flask
app = Flask(__name__)
#app.route('/summary')
def summary():
d = make_summary()
return d, 201 # 200 is the default
You can check the status code using
curl --request GET "http://127.0.0.1:5000/summary" -w "\ncode: %{http_code}\n\n"
The answer is the same when using Flask's class-based views.
from flask import Flask, request, jsonify
from flask.views import MethodView
app = Flask(__name__)
class Summary(MethodView):
def get(self):
d = make_summary()
return jsonify(d)
app.add_url_rule('/summary/', view_func=Summary.as_view('summary'))
if its a dict, flask can return it directly (Version 1.0.2)
def summary():
d = make_summary()
return d, 200
To serialize an object, use jsonify from flask module to jsonify the object, a dictionary gets serialized by default. Also, if you're dealing with files you can always use make_response.
I like this way:
#app.route("/summary")
def summary():
responseBody = { "message": "bla bla bla", "summary": make_summary() }
return make_response(jsonify(responseBody), 200)