This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
repos: | |
- repo: https://github.com/ambv/black | |
rev: 24.2.0 | |
hooks: | |
- id: black | |
name: Code formatting | |
language_version: python3.10 | |
args: [ --config=pyproject.toml ] | |
exclude: | | |
(?x)^( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[tool.black] | |
line-length = 130 | |
target-version = ['py310'] | |
include = '^src/.*\.py$' | |
force-exclude = ''' | |
/( | |
# The following are specific to Black, you probably don't want those. | |
| blib2to3 | |
| tests/data | |
| profiling |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/') | |
def leaderboard(): | |
# Retrieve the top 10 players from the leaderboard and redirect to the leaderboard page with the data | |
leaderboard = get_leaderboard() | |
return render_template('leaderboard.html', leaderboard=leaderboard) | |
@app.route('/submit_score', methods=['POST']) | |
def submit_score(): | |
player_id = request.form['player_id'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_score(player_id, score): | |
# Add or update the score for the player in the leaderboard | |
redis_client.zadd(LEADERBOARD_KEY, {player_id: score}) | |
def get_leaderboard(): | |
leaderboard = redis_client.zrevrange(LEADERBOARD_KEY, 0, 9, withscores=True) | |
formatted_leaderboard = [{'player_id': player_id.decode('utf-8'), 'score': int(score)} for player_id, score in | |
leaderboard] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, render_template, request, redirect, url_for | |
import redis | |
app = Flask(__name__) | |
# Read the connection string from the environment variable | |
UPSTASH_FLY_REDIS_CONNECTION_STRING = os.getenv('REDIS_URL') | |
LEADERBOARD_KEY = 'leaderboard' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponse | |
from celery_redis.celery import reverse_and_sleep | |
from django.urls import path | |
def trigger_task(request): | |
result = reverse_and_sleep.delay("Hello, Celery!", 10) | |
return HttpResponse(f"Task triggered. Task ID: {result.id}") | |
def get_result(request, task_id): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In production, please use os.getenv("REDIS_URL") instead and don't hardcode values here | |
REDIS_URL = '<your_redis_url>' # Replace with your broker URL | |
CELERY_BROKER_URL = REDIS_URL | |
CELERY_RESULT_BACKEND = REDIS_URL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
from celery import Celery | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "celery_redis.settings") # Update with your project's settings module | |
app = Celery("celery_redis") # Update with your project name | |
app.config_from_object("django.conf:settings", namespace="CELERY") | |
app.autodiscover_tasks() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up a python virtual environment and activate it | |
$ python3 -m venv venv | |
$ source venv/bin/activate | |
# Install Django and Celery with Redis | |
$ pip install django celery[redis] | |
# Set up a django project | |
$ mkdir project | |
$ cd project && django-admin startproject celery_redis . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: argoproj.io/v1alpha1 | |
kind: Application | |
metadata: | |
# name of the deployed application to ArgoCD | |
name: fastapi-service-development | |
namespace: argocd | |
spec: | |
# For which project we are creating this app | |
project: fastapi-project | |
source: |
NewerOlder