Skip to content

Commit

Permalink
basic emojivision web app
Browse files Browse the repository at this point in the history
  • Loading branch information
lusbenjamin committed Aug 20, 2017
1 parent 4b4179f commit 5a7e577
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 13 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ jobs:
name: run linters
command: |
source venv/bin/activate
pylint manage.py
pylint pymoji
pylint manage.py pymoji
# Run tests and save results
- run:
Expand All @@ -77,15 +76,16 @@ jobs:

# Deploy successful builds on master to Google App Engine
# https://circleci.com/docs/2.0/deployment_integrations/
# https://cloud.google.com/sdk/gcloud/reference/app/deploy
- deploy:
name: maybe deploy
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
if [ "${CIRCLE_BRANCH}" == "basic-form" ]; then
source ~/google-cloud-sdk/path.bash.inc
gcloud --quiet components update
gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json
gcloud --quiet config set project $GCLOUD_PROJECT_ID
gcloud --quiet app deploy --promote
gcloud --quiet app deploy --promote --stop-previous-version
else
echo 'skipping deployment for non-master branch'
fi
Expand Down
82 changes: 74 additions & 8 deletions pymoji/app.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,90 @@
# -*- coding: utf-8 -*-
from datetime import datetime
import logging
import os

from flask import Flask
from flask import Flask, flash, redirect, render_template, request, url_for
from google.cloud import error_reporting
import google.cloud.logging

from pymoji.constants import OUTPUT_PATH, TEMP_FILENAME, TEMP_PATH
from pymoji.faces import main

RESOURCES = os.path.join('pymoji', 'static')
OUTPUT_DIR = os.path.join(RESOURCES, 'gen')

app = Flask(__name__)
app.config['SECRET_KEY'] = 'so-so-secret' # change this
app.config['PROJECT_ID'] = 'pymoji-176318'

# Configure logging
if not app.testing:
client = google.cloud.logging.Client(project=app.config['PROJECT_ID'])
# Attaches a Google Stackdriver logging handler to the root logger
client.setup_logging(logging.INFO)

@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'πŸ‘‹ 🌎!'

@app.after_request
def after_request(response):
# Quick and dirty hack to wipe out caching for now
response.headers['Last-Modified'] = datetime.now()
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'

# Security-related best practice headers
response.headers.add('X-Frame-Options', 'DENY')
response.headers.add('X-Content-Type-Options', 'nosniff')
response.headers.add('X-XSS-Protection', '1')
return response


@app.route('/emojivision')
def emojivision():
input_image_url = url_for('static', filename=TEMP_FILENAME)
output_image_url = None

if os.path.isfile(OUTPUT_PATH):
output_image_url = url_for('static', filename='gen/face-input-output.jpg')

return render_template('result.html',
input_image_url=input_image_url,
output_image_url=output_image_url)


@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# check if the post request has an image
if 'image' not in request.files:
flash('No image')
return redirect(request.url)
image = request.files['image']
# if user does not select file, browser submits an empty part sans filename
if image.filename == '':
flash('No selected file')
return redirect(request.url)
if image and image.filename:
image.save(TEMP_PATH)
image.close()
main(TEMP_PATH, OUTPUT_PATH)

return redirect(url_for('emojivision'))

return render_template("form.html")


@app.route("/robots.txt")
def robots_txt():
return "User-agent: *\nDisallow: /\n"


@app.errorhandler(500)
def server_error(e):
"""Error handler that reports exceptions to Stackdriver Error Reporting.
Note that this is only used iff DEBUG=False
"""
http_context = error_reporting.build_flask_context(request)
error_client = error_reporting.Client(project=app.config['PROJECT_ID'])
error_client.report_exception(http_context=http_context)
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
Expand Down
29 changes: 29 additions & 0 deletions pymoji/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>βœ¨πŸ“ΈπŸ•Άβœ¨</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="TensorBros: Emojivision">
<link rel="shortcut icon" href="/static/favicon.ico" />
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="/">
<div class="navbar-brand">πŸ“Έ Upload πŸ“Έ</div>
</a>
</div>
<ul class="nav navbar-nav">
<li><a href="/emojivision">βœ¨πŸ•Ά Emojivision πŸ•Άβœ¨</a></li>
</ul>
</div>
</div>
<div class="container">
{% block content %}{% endblock %}
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions pymoji/templates/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}

<h3>πŸ“Έ Upload πŸ“Έ</h3>

<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="image">JPG</label>
<input type="file" name="image" id="image" class="form-control"/>
</div>
<button type="submit" value="Upload" class="btn btn-success">πŸš€ Launch πŸš€</button>
</form>

{% endblock %}
27 changes: 27 additions & 0 deletions pymoji/templates/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "base.html" %}

{% block content %}

<h3>βœ¨πŸ•Ά Emojivision πŸ•Άβœ¨</h3>

{% if input_image_url %}
<div class="media">
<div class="media-left">
<img src="{{input_image_url}}" alt="input image" width="400">
</div>
</div>
{% else %}
<p>🚧 No πŸ“Έ found! 🚧</p>
{% endif %}

{% if output_image_url %}
<div class="media">
<div class="media-left">
<img src="{{output_image_url}}" alt="output image" width="400">
</div>
</div>
{% else %}
<p>🚧 No πŸ“Έ found! 🚧</p>
{% endif %}

{% endblock %}
2 changes: 1 addition & 1 deletion tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def test_index():

r = client.get('/')
assert r.status_code == 200
assert u'πŸ‘‹ 🌎!' in r.data.decode('utf-8')
assert u'βœ¨πŸ“ΈπŸ•Άβœ¨' in r.data.decode('utf-8')

0 comments on commit 5a7e577

Please sign in to comment.