Python Flask Example (POST)

MD
R
Markdown

Sample example of using vritualenv with Flask as a POST endpoint

Instructions

Setup your MacOS Environment

  1. Download the lastest Python from https://www.python.org/downloads/ python3 -V

  2. Installing virtaulenv sudo pip3 install virtualenv virtualenv --version

  3. Create the flask app mkdir ~/project cd ~/projets

  4. Create a virtualenv virtualenv hello_flask cd hello_flask

  5. Activate it source bin/activate

  6. Install Flask pip3 install Flask

  7. Create your sample app (app.py)

import flask
import json
import logging

from flask import request, jsonify

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)

app = flask.Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET'])
def home():
    return '''<h1>Distant Reading Archive</h1>
<p>A prototype API for distant reading of science fiction novels.</p>'''

# A route to return all of the available entries in our catalog.
@app.route('/v1/font2ttf', methods=['POST'])
def api_all():
    hello = request.data
    print(json.loads(hello));
    return 'Hello World';

app.run()

  1. Run the app python app.py

  2. Open browser and send a JSON POST localhost:5000

Created on 10/23/2018