Python Flask Example (POST)
MD
R
MarkdownSample example of using vritualenv with Flask as a POST endpoint
Instructions
Setup your MacOS Environment
-
Download the lastest Python from https://www.python.org/downloads/
python3 -V -
Installing virtaulenv
sudo pip3 install virtualenvvirtualenv --version -
Create the flask app
mkdir ~/projectcd ~/projets -
Create a virtualenv
virtualenv hello_flaskcd hello_flask -
Activate it
source bin/activate -
Install Flask
pip3 install Flask -
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()
-
Run the app
python app.py -
Open browser and send a JSON POST localhost:5000
Created on 10/23/2018