Brian Smith
Brian Smith

Reputation: 55

Python Flask Modifying Page after loaded

I have a question about using Flask with Python. Lets say I want to make a website for some mod I'm making for a game, and I want to put in a live chat feed, how would I go around modifying the contents of the page after the page has been sent to the person?

Upvotes: 1

Views: 2099

Answers (3)

Kugel
Kugel

Reputation: 19824

I would suggest you look into AJAX, specifically jQuery.

jQuery can send ajax request to your flask backend to retrieve more data and upon fetching new data it can modify the html contents of the page in user's browser.

Example:

$.getJSON("/chat-feed", function(msg){
  $("#chat-container").append("<div>" + msg.text + "</div>");
});

Upvotes: 1

Marwan Alsabbagh
Marwan Alsabbagh

Reputation: 26788

That is definitely doable. What you do is load an html page with some javascript that will make calls to your webserver to update the page with recent chat lines. The tutorial Realtime Web Chat with Socket.io and Gevent is a very good place to start. He explains the whole process from the ground up in one article. There are also two other stackoverflow questions that might be useful to you:

For flask specific implementations of a chat application you might want to check out these two projects:

Upvotes: 4

Tom Dalling
Tom Dalling

Reputation: 24115

Short answer: you can't.

Longer answer: once you have "sent the page" (that is, you have completed a HTTP response) there is no way for you to change what was sent. You can, however, use JavaScript to make additional HTTP requests to the server, and use the HTTP responses to modify the DOM which will change the page that the person is looking at. There are many ways to make a live chat feed, all of which are too complicated to put in a single Stack Overflow answer, but you can be sure that they all use JavaScript.

Upvotes: 3

Related Questions