Code Monkey home page Code Monkey logo

flask-pywren's Introduction

Example Pywren Flask REST API

Pywren Hello World REST API.

There are three routes in this example,

  • POST /pywren-hello-world
  • POST /pywren-hello-world-celery
  • GET /pywren-hello-world-celery/{task_id}

Navigate to localhost:5000 and you can test these routes with swaggar. The /pywren-hello-world-celery will return a task_id which you can copy/paste (copy withough the quotes) in /pywren-hello-world-celery/{task_id} which will return the result.

The basis for the example routes can be found here.

Getting Started

Quickly set up an environment by running pipenv install and pipenv install --dev. The --dev flag will add linting, testing and an ipython terminal.

Setting up Pywren

Either in a .env file or by setting environ variables add the following,

FLASK_APP='app.run'
FLASK_ENV='development'
PYWREN_STORAGE_BUCKET=<BUCKET_NAME>
PYWREN_IBM_CF_ENDPOINT=<HOST>
PYWREN_IBM_CF_NAMESPACE=<NAMESPACE>
PYWREN_IBM_CF_API_KEY=<API_KEY>
PYWREN_IBM_COS_ENDPOINT=<REGION_ENDPOINT>
PYWREN_IBM_COS_API_KEY=<API_KEY>

for more information see the official docs

Running

Once you've set you pywren config variables, you can activate your environment with pipenv shell, and run the application with flask run.

You can then naviagte to the home route http://localhost:5000 which will redirect you to http://localhost:5000/api/v1/

Getting data with requests.

You can process the endpoints with CURL or with python using the requests library, here is a simple example with requests.

import requests
r = requests.post("http://localhost:5000/api/v1/pywren-hello-world", json={'iterdata': [1,2,3,4]})
data = r.json()
print(data)
>>> {'result': 38}

Install Redis docker image

These are the instructions for installing a Redis docker container and running it. This is espically nice on windows because you don't have to worry about all the dependencies.

This guide assumes that you already have docker installed.

Run the following,

docker pull redis
docker run --name unique-name -d redis

This will expose port 6379 on your localhost. You can now set your celery backend to backend='redis://127.0.0.1'.

Source

Install rabbitmq docker image

Install a rabbitmy docker image by

docker run -d --hostname flask-rabbit --name flask-rabbit -p 4369:4369 -p 5671:5671 -p 5672:5672 rabbitmq:3

Then in your celery config set you broker as broker='amqp://localhost:5672'

Source

Start Celery workers

Check out this SO to see to to run Celery 4.3 in Windows 10.

Run demo app: celery -A scripts.celery_demo worker -l info -P gevent

To run the flask celery worker: celery -A celery_entrypoint worker -l info -P gevent

Docker Compose

There is a docker-compose.yml file where you can quickly start and stop all the running containers. First create a docker.env file with the same contents as the .env file.

To build all the necessary containers run,

docker-compose build

To run it,

docker-compose run -d

Then once everything is build you can check the status of the containers by,

docker-compose ps

Then just navigate to localhost and from there you can test out the apis with swaggar.

To stop all the running containers,

docker-compose down

flask-pywren's People

Contributors

spitfiredd avatar

Watchers

James Cloos avatar  avatar

Forkers

webclinic017

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.