Skip to content

Instantly share code, notes, and snippets.

View vjanz's full-sized avatar
🎯
Focusing

Valon Januzaj vjanz

🎯
Focusing
View GitHub Profile
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)^(
[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
@vjanz
vjanz / app.py
Created November 30, 2023 21:43
@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']
@vjanz
vjanz / app.py
Created November 30, 2023 21:31
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]
@vjanz
vjanz / app.py
Last active December 7, 2023 09:37
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'
@vjanz
vjanz / urls.py
Created October 10, 2023 23:12
Urls.py
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):
@vjanz
vjanz / settings.py
Created October 10, 2023 23:10
Celery settings
# 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
@vjanz
vjanz / celery.py
Created October 10, 2023 23:06
Celery configuration
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()
@vjanz
vjanz / instructions.bash
Last active October 10, 2023 22:05
Setting up the Django project
# 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 .
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: