Category : flask

Let’s say I want users to complete an action every day, so I keep track of a current_streak and longest_streak. current_streak = get_current_streak(user_id) longest_streak = get_longest_streak(user_id) Instead of recomputing the longest_streak every time the user asks for it, I cache it indefinitely, and recompute it only when I see that current_streak exceeds the longest_streak @cache.memoize(timeout=99999) ..

Read more

I am building a multi-page application with Dash. My goal is to based on the request header load the data in each application. The structure of my application looks like this: apps/app_1.py apps/app_2.py apps/app_3.py index.py app.py I need to have an access to flask.request.headers at the moment when I am loading data in the given ..

Read more

The issue has appeared recently and the previously healthy container now enters a sleep loop when a shutit session is being created. The issue occurs only on Cloud Run and not locally. Minimum reproducible code: requirements.txt Flask==2.0.1 gunicorn==20.1.0 shutit Dockerfile FROM python:3.9 # Allow statements and log messages to immediately appear in the Cloud Run ..

Read more

Using the python socketio package, I created a timer that starts when a client connects to my server. Server-side code: import socketio sio = socketio.Server() app = socketio.WSGIApp(sio, static_files={ ‘/’: ‘./public/’ }) @sio.event def connect(sid, environ): count = 10 while count > 0: sio.emit(‘timer_count’, count) sio.sleep(1) count -= 1 HTML code for reference (index.html): <!doctype ..

Read more

I successfully receive a 200 status response when calling a REST API through Python 3.9.x’s request module: r = requests.get(URL, headers={"Accept": "application/json"} However, when I call this same requests line within a Flask function, I receive a 406 error with the following error messages from the REST API: {‘Date’: ‘Tue, 21 Sep 2021 01:33:02 GMT’, ..

Read more

I’m running python code that’s similar to: import numpy def get_user_group(user, groups): if not user.group_id: user.group_id = assign(groups) return user.group_id def assign(groups): for group in groups: ids.append(group.id) percentages.append(group.percentage) # e.g. .33 assignment = numpy.random.choice(ids, p=percentages) return assignment We are running this in the wild against tens of thousands of users. I’ve noticed that the assignments ..

Read more

I want to clear the session if someone clicks on the Home button or Site name. Home Link: <a href="{{ url_for(‘index’) }}" ><h1>Home</h1></a> I tried this <a href="{{ session.clear(); url_for(‘index’) }}" ><h1>Home</h1></a> But this errored out. Is there any way to include the session clear in this jinja2 template for the home link? Source: Python ..

Read more