Skip to main content

RESTful Flask framework with MongoDB, Redis and Celery integrations

Project description

🙈 Dead Simple Web Framework 🙉

Python Flask MongoDB/Pymongo PyPi

Overview:

A RESTful Flask framework with MongoDB, Redis and Celery integrations:

Backend:

  • Flask [Framework]
  • MongoDB [Database]
  • Redis [Cache]
  • Celery + RabbitMQ [Async Tasks]

Installing:

$ pip install dead-simple-framework

Configuration:

TODO - DOCS

Example Application (demo.py):

* Celery must be running *

$ celery -A demo worker -l info
from dead_simple_framework import Application, Task_Manager, Database, API, Route, RouteHandler, DefaultRouteHandler, Task
from random import choice

sample_config = {
    'routes': {
        'insert': Route( # A route with automatic CRUD support
            url='/insert',
            collection='insert',
        ),
        'demo': Route( # Another route with automatic CRUD support (with all options specified)
            url='/demo',                    # The URL at which the server should handle requests
            defaults=None,                  # Default parameters to pass to the handler function for this URL
            database='db',                  # The database that should be passed to the handler function for this URL 
            collection='demo',              # The collection that should be passed to the handler function for this URL 
            handler=DefaultRouteHandler()   # A class specifying the handler functions to use based on the method used to access the URL 
        ),
        'refresh': Route(  # Route with a handler for the GET method that runs an async task (in this case an API call)
            url='/api/refresh',
            handler=RouteHandler(
                GET=lambda request, payload: str(Task_Manager.run_task('scheduled_call'))
            )
        ),
        'index': Route(  # Route that fetches the cached latest result of an async task (in this case an API call)
            url='/',
            handler=RouteHandler(
                GET=lambda request, payload: str(Task_Manager.get_result('scheduled_call'))
            )
        ),
        'add': Route(  # Route that runs an async task (in this case simple addition) and fetches the result
            url='/add',
            handler=RouteHandler(
                GET=lambda request, payload: str(Task_Manager.run_task('add'))
            )
        ),
    },

    'tasks': { # Async tasks available to the Task_Manager [celery] to schedule or run
        'add': {        # Simple Addition Task (with default arguments) 
            'logic': lambda x,y: x + y,
            'schedule': None,
            'default_args': (2,2)
        },
        'insert': {     # Periodic Database Insert Task 
            'logic': lambda res: Database(collection='insert').connect().insert_one({'test': 'doc', 'result': res}),
            'schedule': {}, # Default - every minute
            'depends_on': 'add' # Return value substituted for `res`
        },
        'call_api': {   # API Call Task
            'logic': lambda url, params=None: str(API.get(url, params, ignore_errors=False, retry_ms=10000, num_retries=20).content),
        },
        'scheduled_call': Task( # Make 3 parallel API calls
            name='scheduled_call',
            logic=lambda: Task_Manager.parallelize([['call_api', [x]] for x in get_websites(3)]),
            schedule={}
        )
    }
}


WEBSITES =[
    "http://www.google.com",
    "http://www.yahoo.com",
    "http://www.ebay.com",
    "http://www.cnn.com",
    "http://www.pbs.org",
    "https://www.reddit.com/",
]

def get_websites(n):
    _websites = [*WEBSITES]
    res = []
    for x in range(n):
        res.append(choice(_websites))
        _websites.remove(res[-1])

    return res


app = Application(sample_config)
if __name__ == '__main__':
    app.run()
  • Starts a local server at http://0.0.0.0:5000/

  • Serves CRUD operations for MongoDB collection demo at endpoint /demo

  • Runs an asynchronous chained value calculation and insert into the insert collection (viewable at /insert)

  • Runs asynchronous parallel API calls and displays the latest result at endpoint / (can be manually run at /api/refresh)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dead_simple_framework-0.9.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dead_simple_framework-0.9.0-py3-none-any.whl (41.6 kB view details)

Uploaded Python 3

File details

Details for the file dead_simple_framework-0.9.0.tar.gz.

File metadata

  • Download URL: dead_simple_framework-0.9.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for dead_simple_framework-0.9.0.tar.gz
Algorithm Hash digest
SHA256 231998f05c8c26c716ceed47a04c3c2fcce078c8420483663a61860a9b5044c5
MD5 f792d6bb763c93eb7467b6d53c82c575
BLAKE2b-256 25835076e0468dc3f34e80d5740b725c2724e3c429124537db5ad939a39f077f

See more details on using hashes here.

File details

Details for the file dead_simple_framework-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: dead_simple_framework-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 41.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for dead_simple_framework-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc96f7d70e422d4ee0b345c6353c18630e870e0aa550c4ddbc4242bb3bb4b7dd
MD5 84e14187dab72d158fb7eb1faf014715
BLAKE2b-256 d525ecd8293d56e1f1269d9ca9ebf6591053a3b0b31b8656e0ecb2c1c21745d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page