![simple things miguel video simple things miguel video](https://i.pinimg.com/originals/35/b7/b5/35b7b501f390302b07a285876277e1b5.jpg)
For the purpose of having a stream where each part replaces the previous part the multipart/x-mixed-replace content type must be used. There are several multipart content types for different needs.
![simple things miguel video simple things miguel video](https://i.pinimg.com/originals/5c/52/5c/5c525c9ddf72eef033e9f355d7f12e7d.jpg)
Multipart responses consist of a header that includes one of the multipart content types, followed by the parts, separated by a boundary marker and each having its own part specific content type. The secret to implement in-place updates is to use a multipart response. With this technique you can have each chunk in the stream be an image, and that gives you a cool video feed that runs in the browser!
#SIMPLE THINGS MIGUEL VIDEO HOW TO#
This is a good example of how to generate large responses, but something a little bit more exciting is to work with real time data.Īn interesting use of streaming is to have each chunk replace the previous one in the page, as this enables streams to "play" or animate in the browser window.
![simple things miguel video simple things miguel video](https://www.thesuperid.com/wp-content/uploads/2014/06/Miguel_Simplethings_Featured-Image.jpg)
The table example above generates a traditional page in small portions, with all the parts concatenated into the final document.
#SIMPLE THINGS MIGUEL VIDEO GENERATOR#
Flask then takes care of invoking the generator and sending all the partial results as chunks to the client.įor this particular example if you assume () returns the result of a database query as an iterable, then you can generate a potentially large table one row at a time, so regardless of the number of elements in the query the memory consumption in the Python process will not grow larger and larger due to having to assemble a large response string. A route that returns a streamed response needs to return a Response object that is initialized with the generator function. In this example you can see how Flask works with generator functions. Yield render_template('stock_row.html', stock=stock) Yield render_template('stock_header.html') The example below shows how using streaming it is possible to generate a large data table, without having to assemble the entire table in memory: from flask import Response, render_template Flask uses this characteristic of generator functions to implement streaming. You can see in this simple example that a generator function can return multiple results in sequence. Describing how generator functions are implemented is outside the scope of this article, but if you are a bit curious the following shell session will give you an idea of how generators are used: > x = gen() This is a function that runs in three steps, each returning a value. Consider the following function: def gen(): A generator is a special function that can be interrupted and resumed. A lot of security cameras use this technique to stream video to web browsers.įlask provides native support for streaming responses through the use of generator functions. A pretty good example of this is a real time video or audio feed. For some applications a request may need to return data that comes from a real time source. Providing the response in small portions is a much better solution, assuming the data can be generated in chunks. An alternative would be to write the response to disk and then return the file with nd_file(), but that adds I/O to the mix. Having to assemble a response in memory only to return it to the client can be inefficient for very large responses. I can think of a couple of reasons why this might be useful: Streaming is a technique in which the server provides the response to a request in chunks. NOTE: there is now a follow-up to this article, Flask Video Streaming Revisited, in which I describe some improvements to the streaming server introduced here. To illustrate the topic I'm going to show you how to build a live video streaming server! This article is dedicated to streaming, an interesting feature that gives Flask applications the ability to provide large responses efficiently partitioned in small chunks, potentially over a long period of time. While the coverage of the Flask framework in these is fairly complete, there are a small number of features that for one reason or another did not get mentioned much, so I thought it would be a good idea to write articles about them here. I'm sure by now you know that I have released a book and a couple of videos on Flask in cooperation with O'Reilly Media.