-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
35 lines (25 loc) · 755 Bytes
/
server.py
File metadata and controls
35 lines (25 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import Flask
from flask import send_file
from threading import Thread
from io import BytesIO
import wcloud
from twitter_fetch import fetch_tweets
app = Flask(__name__)
@app.route('/')
def static_from_file():
with open('input.txt', 'r') as file:
text = file.read().replace('\n', '')
image = wcloud.render(text)
return serve_pil_image(image)
@app.route('/<username>')
def index(username):
text = fetch_tweets(username)
image = wcloud.render(text)
return serve_pil_image(image)
def serve_pil_image(pil_img):
img_io = BytesIO()
pil_img.save(img_io, 'PNG', quality=70)
img_io.seek(0)
return send_file(img_io, mimetype='image/png')
if __name__ == '__main__':
Thread(target=app.run).run()