# Concurrent web video streaming with Dash (and Flask)

**URL:** <https://community.plotly.com/t/concurrent-web-video-streaming-with-dash-and-flask/34603>\
**Category:** Dash Python\
**Created:** [February 6, 2020, 3:17pm UTC](https://community.plotly.com/t/concurrent-web-video-streaming-with-dash-and-flask/34603 "2020-02-06T15:17:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clockzarb](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@Clockzarb](https://community.plotly.com/u/Clockzarb)\
**Post date:** [February 6, 2020, 3:17pm UTC](https://community.plotly.com/t/concurrent-web-video-streaming-with-dash-and-flask/34603/1 "2020-02-06T15:17:50Z")

</div>

I’m trying to achieve something similar to this : [Does Dash support opencv video from webcam?](https://community.plotly.com/t/does-dash-support-opencv-video-from-webcam/11012)

I want to be able to stream videos from a server to clients, but having an individual video stream for each user, not a shared one. Is there a way to do that with Plotly Dash ?

Thank you

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [February 6, 2020, 4:51pm UTC](https://community.plotly.com/t/concurrent-web-video-streaming-with-dash-and-flask/34603/2 "2020-02-06T16:51:59Z")

</div>

Hi @Clockzarb, Dash is meant to work well with concurrent sessions as long as you don’t use global variables (see [https://dash.plot.ly/sharing-data-between-callbacks](https://dash.plot.ly/sharing-data-between-callbacks), “Why global variables will break you app”). So if you have a dropdown or input component to choose the URL of the stream it should work well. Did you give it a try? You can check it by opening two different browsers on your local machine for example.

---

<div class="post-metadata">

**Author:** ![annastasia](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@annastasia](https://community.plotly.com/u/annastasia)\
**Post date:** [September 28, 2020, 1:23pm UTC](https://community.plotly.com/t/concurrent-web-video-streaming-with-dash-and-flask/34603/3 "2020-09-28T13:23:19Z")

</div>

## Implementing Streaming With Flask

Flask provides native support for streaming responses through the use of [generator functions](http://legacy.python.org/dev/peps/pep-0255/). A generator is a special function that can be interrupted and resumed. Consider the following function:

```auto
def gen():
    yield 1
    yield 2
    yield 3

```

This is a function that runs in three steps, each returning a value. 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:

```auto
>>> x = gen()
>>> x
<generator object gen at 0x7f06f3059c30>
>>> next(x)
1
>>> next(x)
2
>>> next(x)
3
>>> next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

```

You can see in this simple example that a generator function can return multiple results in sequence. Flask uses this characteristic of generator functions to implement streaming.

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:

```auto
from flask import Response, render_template
from app.models import Stock

def generate_stock_table():
    yield render_template('stock_header.html')
    for stock in Stock.query.all():
        yield render_template('stock_row.html', stock=stock)
    yield render_template('stock_footer.html')

@app.route('/stock-table')
def stock_table():
    return Response(generate_stock_table())

```

In this example, you can see how Flask works with generator functions. A route that returns a streamed response needs to return a `Response` object that is initialized with the generator function. Flask then takes care of invoking the generator and sending all the partial results as chunks to the client.

For this particular example if you assume `Stock.query.all()` 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.

Otherwise, you should create your own video streaming platform with specific plugins for different business modules here find out the top global service provider to make with advanced features here [https://hackernoon.com/how-to-create-a-video-on-demand-website-like-netflix-youtube-f238fbcd5e09](https://hackernoon.com/how-to-create-a-video-on-demand-website-like-netflix-youtube-f238fbcd5e09)

Hope it will be useful for on demand project developers and content distributors
