mardi 9 février 2021

Pass a paseudo-random values which must be changed when user goes away from page and stay the same when user refreshs page

I'm working on web app and I'm using Flask. I need to set my values in page as pseudo-random and out of page as random. So when I refresh page (in page option) values must stay the same and opposite when I was redirected.
Check the example below.

routes.py

...
@app.route('/some_page', methods=['GET', 'POST'])
@login_required
def page():
    values = form_values(number=3) # This function returns a random value
    return render_template('page.html',values=values)

function.py

def form_values(number:int) -> dict:
    result = {}
    for i in range(0, number):
        value = round(random.uniform(100,500), 2)
        result[i] = package
    return result

I've read about random.seed() but there is a problem. With seed I fix value forever, that is when I go away from page and return this values remain the same (what I don't want to).

I also tried to set random state in previous route. Main idea was that I pass random.seed(None) in previous route, then I get state in next route with random.getstate() and pass with random.setstate(). So when user redirects to page he uses the same random state, and when he comes from previous page his state is generated again. But it seems routes don't relate with each other, so attempt was failed.

!NOTE: I'm not good at JS or PHP, so if it's possible, I'd like to know solution without them.




Aucun commentaire:

Enregistrer un commentaire