Create placeany flask project

This commit is contained in:
Anders Englöf Ytterström 2024-09-03 00:52:21 +02:00
commit 5152af4126
6 changed files with 221 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
venv
__pycache__
.ropeproject
images

5
LICENSE Normal file
View file

@ -0,0 +1,5 @@
"THE FADEOEL LICENSE" (Revision 4):
Anders Englöf Ytterström wrote this file. As long as you retain this notice
you can do whatever you want with this stuff, but do know that any
problem is YOUR problem, not hers/his/theirs. If you meet some day, and you
think this stuff is worth it, you must buy her/him/them a beer in return.

12
requirements.txt Normal file
View file

@ -0,0 +1,12 @@
blinker==1.8.2
cachelib==0.9.0
click==8.1.7
Flask==3.0.3
Flask-Cache==0.13.1
Flask-Caching==2.3.0
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==2.1.5
pillow==10.4.0
waitress==3.0.0
Werkzeug==3.0.4

View file

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>placeany bookmarklet</title>
<style>
body {
width: 900px;
margin: 100px auto;
}
img {
display: block;
}
.line {
overflow: hidden;
}
.unit {
float: left;
width: 50%;
padding-bottom: 10px;
}
footer {
font-size: small;
text-align: center;
}
</style>
</head>
<body>
<div role="main" class="line">
<div class="unit">
<h1>placeany bookmarklet</h1>
<p>A bookmarklet which add your flavor of choice to any website by replacing images with placeholder images from
this <a href="/">placeany</a> instance.</p>
<ul>
<li><a
href="{% raw %}javascript:/*jslint%20indent:%202,%20browser:%20true%20*/var%20madrHoldr%20=%20function%20()%20{%22use%20strict%22;var%20images,%20max,%20i,%20src,%20img,theme%20=%20%22{% endraw %}{{ theme }}{% raw %}%22,url%20=%20%22http://{% endraw %}{{ url }}{% raw %}%22;images%20=%20document.getElementsByTagName(%22img%22);for%20(i%20=%200,max%20=%20images.length;i%3Cmax;i++)%20{if%20(images[i].width)%20{img%20=%20images[i];src%20=%20img.width%20+%20%22/%22%20+%20img.height;src%20=%20url%20+%20%22/%22%20+%20src;img.src%20=%20src;}}};madrHoldr();{% endraw %}">placeanyize!</a>
</li>
</ul>
</div>
<div class="unit">
<h2>Instructions</h2>
<ol>
<li>Drag bookmarklet to your bookmarks bar. <br><small>(You may need to go to your browser's view menu and make
sure the
Bookmarks toolbar is displayed.)</small></li>
<li>Navigate to any website. <br><small>(The more images, the better.)</small></li>
<li>Click the bookmark.</li>
</ol>
</div>
</div>
<hr>
<footer role="contentinfo">
Also available as a <a href="/">Placeholder Service</a>.<br>
Source on <a href="https://github.com/madr/placeany">GitHub</a>.
</footer>
</body>
</html>

70
templates/index.html Normal file
View file

@ -0,0 +1,70 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>placeany</title>
<style>
body {
width: 900px;
margin: 100px auto;
}
img {
display: block;
}
.line {
overflow: hidden;
}
.unit {
float: left;
width: 50%;
padding-bottom: 10px;
}
footer {
font-size: small;
text-align: center;
}
</style>
</head>
<body>
<div role="main" class="line">
<article class="unit">
<h1>placeany</h1>
<p>A quick and simple service for getting pictures for use as placeholders in your designs or code.
Just put your image size (width &amp; height) after our URL and you'll get a placeholder.</p>
<pre><output>Examples:
<a href="/200/300">{{ url }}/200/300</a>
<a href="/g/200/300">{{ url }}/g/200/300</a>
</output></pre>
</article>
<div class="unit">
<img src="/450/230" alt="">
</div>
</div>
<div class="line">
<div class="unit">
<img src="440/440" alt="">
</div>
<div class="unit">
<div class="unit">
<img src="215/175" alt="">
</div>
<div class="unit">
<img src="225/175" alt="">
</div>
<img src="450/255" alt="">
</div>
</div>
<hr>
<footer role="contentinfo">
Also available as a <a href="/bookmarklet">Bookmarklet Service</a>.<br>
Source on <a href="https://github.com/madr/placeany">GitHub</a>.
</footer>
</body>
</html>

66
wsgi.py Normal file
View file

@ -0,0 +1,66 @@
import os
import random
from io import BytesIO
from flask import Flask, render_template, request, send_file
from flask_caching import Cache
from PIL import Image, ImageOps
app = Flask(__name__)
GREY = "G"
COLOR = "RGB"
cache = Cache(config={"CACHE_TYPE": "SimpleCache"})
app = Flask(__name__)
cache.init_app(app)
def get_cropped_image(x, y, grey=False):
"""crops a random image from collection"""
im_src = random.choice(os.listdir("./images"))
im = Image.open(f"images/{im_src}")
out = BytesIO()
max_x, max_y = im.size
if x < max_x or y < max_y:
im = ImageOps.fit(im, (x, y))
if grey:
im = ImageOps.grayscale(im)
im.save(out, "WEBP", quality=50)
out.seek(0)
return out
def make_response(x, y, color_mode=COLOR):
im = get_cropped_image(x, y, color_mode == GREY)
return send_file(im, mimetype="image/webp")
@app.route("/")
def hello():
u = request.host
return render_template("index.html", url=u)
@app.route("/bookmarklet")
def bookmarklet():
u = request.host
return render_template("bookmarklet.html", url=u)
@app.route("/<int:x>/<int:y>")
@cache.cached(10)
def generate(x, y):
return make_response(x, y, COLOR)
@app.route("/g/<int:x>/<int:y>")
@cache.cached(10)
def generate_grey(x, y):
return make_response(x, y, GREY)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port)