Skip to content

Instantly share code, notes, and snippets.

@morrisalp
Created August 23, 2020 17:59
Show Gist options
  • Save morrisalp/c782d7f8e625bcf2cc589bfc4c5967ea to your computer and use it in GitHub Desktop.
Save morrisalp/c782d7f8e625bcf2cc589bfc4c5967ea to your computer and use it in GitHub Desktop.
demo of flask_caching - calculating pi with ζ(2)
from flask import Flask
from flask_caching import Cache
app = Flask(__name__)
app.config.from_mapping({"CACHE_TYPE": "simple"})
cache = Cache(app)
def approximate_pi(n):
output = 0
for i in range(1, n):
output += i ** (-2)
return (6 * output) ** 0.5
@app.route("/<int:n>")
@cache.cached(timeout=50)
def index(n):
return f'Pi is approximately: {approximate_pi(n)}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment