Using Param with Panel breaks my chart

Hi @Marc,

You almost gave me a shock. I read your title as Using Param with Panel breaks my heart :smile:

But it did though, I thought it wouldn’t work :joy:

So in my example below I have created a seperate SharedBuffer class that is shared among all user sessions.

I used your code and now it works like a charm!

I am not an expert in using Tornado, PeriodicCallback and @gen.coroutine so whether that could be modernized using pn.state.schedule_task and async as in How to make the pn.state.schedule_task non-blocking? - Panel - HoloViz Discourse might be worth checking out. But your code works, so maybe its not worth it.

Interesting, I didn’t implemented it yet, but i’ll give it a try.

As your code example was not a minimum, reproducible example I can run I cannot say anything about the throttling and cpu usage.

I understand: the only part you’ll be missing is my marvellous * ahem * ChatGPT generated * ahem* API for tests.

import time
import redis
import json
import math
import random
from flask import Flask, request
import threading

app = Flask(__name__)
redis_client = redis.Redis()

NUM_DATA_POINTS = 1000
sine_params = {'freq': 1.0, 'amplitude': 1.0, 'noise_amplitude': 0.1}

def generate_data():
    while True:
        timestamp = int(time.time() * 1000)
        sine_data = {'timestamp': timestamp}
        for i in range(8):
            sine_var = 'var_{}'.format(i)
            sine_val = float(sine_params['amplitude']) * math.sin(float(sine_params['freq'] + i/5.0) * timestamp / 1000.0) + i
            sine_val += random.uniform(-sine_params['noise_amplitude'], sine_params['noise_amplitude'])
            sine_data[sine_var] = sine_val
        redis_client.rpush('random_sin', json.dumps(sine_data))

        if redis_client.llen('random_sin') >= NUM_DATA_POINTS:
            redis_client.lpop('random_sin')
        time.sleep(0.1)

@app.route('/set_sine_params', methods=['POST'])
def set_sine_params():
    data = request.get_json()
    sine_params['freq'] = data.get('freq', sine_params['freq'])
    sine_params['amplitude'] = data.get('amplitude', sine_params['amplitude'])
    sine_params['noise_amplitude'] = data.get('noise_amplitude', sine_params['noise_amplitude'])
    return 'Sine parameters updated'

if __name__ == '__main__':
    generator_thread = threading.Thread(target=generate_data)
    generator_thread.start()
    app.run()

You might also find some inspiration in the Panel Streaming Gallery

Very interesting read indeed, I didn’t dig enough to find this resource!

Your example might also be a valid addition to the Gallery once polished?

If that can be of any help, I’ll definitely do it.

And lastly, thanks for your huge support!

1 Like