Answer the question
In order to leave comments, you need to log in
Flask API and Chat. How to implement, where to dig?
Hello guys.
I am doing a test.
Need help on the steps and methods of chat implementation, where the REST API implemented in Flask acts as a backend.
I somehow dealt with api on DRF (Django Rest Framwork), but I worked purely with the backend and api. It was somehow simpler there - a request came from the client, the server sent a response. And how the front-end client works there didn’t really bother me. And here by me, according to the instructions, the front-end client should also be implemented on Flask.
I started to implement it, but somehow I got confused how this whole thing should turn.
Now I have several views. But the problem is that they use templates to render pages on the client. That is, everything is mixed up in a heap and there is no separation of the client and backend api.
For example, let's take the stage with registration: there is an address 127.0.0.1/register
When you enter this page, a form with fields is displayed. Further, after the user clicks on the send button, the view performs a simple validation, enters the data into the database, sets the session cookie for the client, and redirects to the main chat page via a redirect.
And everything would be fine, but according to the instructions, everything should be a little different. In theory, the client should have sent json with a request to 127.0.0.1/api/register where the server should have sent something in response. And then something has to happen.
This is exactly how I would like to implement this task, but I don’t understand at all how to implement such a front work with an apish.
At the moment, the view code looks like this pastebin.com/UGt8qPhU
I would be very grateful for tips and advice.
Answer the question
In order to leave comments, you need to log in
In order for the server to issue json, and not render templates, it is enough just to return jsonify() via flask.
For example, I have this
from flask import jsonify
# Begin AJAX requests
@cart.route("/api/total")
def total():
count = 0
if "cart" in session:
cart = session.get("cart")
l = [int(i) for i in redis.hvals(getCartKey(cart))]
count = sum(l)
logger.debug('cart %s : %d', cart, count)
data = calculateItems()
data.pop("products")
return jsonify(dict(count=count, **data))
"""Creates a :class:`~flask.Response` with the JSON representation of
the given arguments with an `application/json` mimetype. The arguments
to this function are the same as to the :class:`dict` constructor.
Example usage::
from flask import jsonify
@app.route('/_get_current_user')
def get_current_user():
return jsonify(username=g.user.username,
email=g.user.email,
id=g.user.id)
This will send a JSON response like this to the browser::
{
"username": "admin",
"email": "[email protected]",
"id": 42
}
For security reasons only objects are supported toplevel. For more
information about this, have a look at :ref:`json-security`.
This function's response will be pretty printed if it was not requested
with ``X-Requested-With: XMLHttpRequest`` to simplify debugging unless
the ``JSONIFY_PRETTYPRINT_REGULAR`` config parameter is set to false.
..versionadded::0.2
"""
https://classroom.udacity.com/courses/ud388/
https://blog.miguelgrinberg.com/post/designing-ar...
shop.oreilly.com/product/0636920034803.do - there is a root tracker
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question