ml-tooling/opyrator

features for production

fredzannarbor opened this issue · 3 comments

This looks great and my first thought was, wow, this looks like a great alternative to Flask! but how would i implement things like authentication & connections to a database -- things that would likely be needed for any real "production" implementation?

Thanks for the nice feedback @fredzannarbor!
In the current alpha version, you could add a token field to the Input object for passing an authentication token.
With regards to databases, you can connect to them in your opyrator functions. Since in the current version only a single function is allowed, you would need two files for a storing / loading example. The following example illustrates how this could look like:

Opyrator to save data

app_save.py

# app_save.py
from pydantic import BaseModel
import time
import sqlite3

db_name = 'example.db'
try:
    con = sqlite3.connect(db_name)
    cur = con.cursor()
    cur.execute('CREATE TABLE messages (timestamp text, message text)')
    con.commit()
    con.close
except Exception:
    pass


class Input(BaseModel):
    message: str
    token: str


class Output(BaseModel):
    status: str


def save(input: Input) -> Output:
    if input.token != 'foo':
        return Output(status="Not authenticated")

    con = sqlite3.connect(db_name)
    cur = con.cursor()

    cur.execute(f"INSERT INTO messages VALUES ('{int(time.time())}', '{input.message}')")
    con.commit()
    con.close()

    return Output(status="saved")
Opyrator to load data

app_load.py

from pydantic import BaseModel
from typing import List
import sqlite3


class Input(BaseModel):
    pass


class Output(BaseModel):
    messages: List[str]


def load(input: Input) -> Output:
    con = sqlite3.connect('example.db')
    cur = con.cursor()
    messages = []
    for row in cur.execute('SELECT * FROM messages ORDER BY timestamp'):
        messages.append(row[1])
    con.close()

    return Output(messages=messages)

Start them via:

opyrator launch-ui app_save:save --port 8080
opyrator launch-ui app_load:load --port 8081

Thank you again for awesome work, it will be really great if opyrator has in-built support of SQL alchemy or any ORM.

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 14 days