Skip to main content

A framework for creating Frame apps in an intuitive and declarative manner

Project description

sufficient

A framework for creating Frame apps in an intuitive and declarative manner.

PyPI - Version PyPI - Python Version


Installation

pip install sufficient

Quick Start

Code snippets here are excerpted from gm-universe under repo frame-app-examples, you may also want to check it.

  1. Create a python project with a directory structure looks like this.
api
└── index.py
frame
├── app.py
├── static
│   ├── features.png
│   ├── home.png
│   ├── howitworks.png
│   ├── howitworks_deploy.png
│   ├── howitworks_programming.png
│   └── unexpected.png
└── templates
    ├── features.svg
    ├── features_casters.svg
    ├── features_chaindata.svg
    └── features_reactions.svg
  1. Define your frame app in frame/app.py.
from sufficient.frames import *


class App:
    name = "GM Universe"
    description = "Greetings from your first frame app using sufficient-py"
    image = "{uri}/static/home.png"
    uri = "{uri}"
    start = "PageHome"


class PageHome:
    def view(self, action: Action, result: ActionResult):
        return ImageFile("home.png")

    def btn_explore(self, action: Action):
        return "PageFeatures"


class PageFeatures:
    def view(self, action: Action, result: ActionResult):
        if "PageHome.btn_explore" == action.source:
            return ImageFile("features.png")
        elif "PageFeatures.btn_casters" == action.source:
            return SvgTemplate("features_casters.svg", result)
        elif "PageFeatures.btn_reactions" == action.source:
            return SvgTemplate("features_reactions.svg", result)
        elif "PageFeatures.btn_chain_data" == action.source:
            return SvgTemplate("features_chaindata.svg", result)
        else:
            return ImageFile("unexpected.png")

    def btn_casters(self, action: Action):
        c = FarcasterClient()
        users = c.neynar_get_users_bulk([action.actor, action.caster])
        actor_pfp = users[0]["pfp_url"]
        actor_name = users[0]["display_name"]
        caster_pfp = users[1]["pfp_url"]
        caster_name = users[1]["display_name"]
        return "PageFeatures", ActionResult(actor_name=actor_name,
                                            actor_pfp=actor_pfp,
                                            caster_name=caster_name,
                                            caster_pfp=caster_pfp)
  1. In api/index.py, create routes as endpoints of your frame server.
from flask import Flask, request, send_from_directory, redirect
import os
import io
import json
from sufficient.frames import FrameAppRunner
from frame import app as frame_app


app = Flask(__name__, instance_relative_config=True)

static_dir = os.path.abspath("frame/static")
templates_dir = os.path.abspath("frame/templates")
# data_dir = app.instance_path
data_dir = "/tmp/data"

runner = FrameAppRunner(frame_app, static_dir,
                        templates_dir, data_dir=data_dir)
try:
    os.makedirs(data_dir)
except OSError:
    pass


@app.route('/')
def frame_index():
    framelet = runner.start()
    return runner.gen_frame_html(framelet, request.host_url, og=True)


@app.route('/static/<string:path>')
def frame_static(path):
    return send_from_directory(static_dir, path)


@app.route('/view/<string:path>')
def frame_image(path):
    return send_from_directory(data_dir, path)


@app.route('/<string:page>/click', methods=['POST'])
def frame_click(page):
    tag, value = runner.click(page, request.json)
    if tag == "framelet":
        return runner.gen_frame_html(value, request.host_url)
    elif tag == "redirection":
        return redirect(value, code=302)
  1. Run your app locally for testing
python3 -m venv venv
source venv/bin/activate
pip install sufficient flask
python -m flask --app api.index run
  1. Use ngrok to make your local server publically accessible
ngrok http http://localhost:5000
  1. Validate your frame app
  1. Deploy

The following instructions are assuming you are going to deploy on vercel. see https://vercel.com/docs/functions/serverless-functions/runtimes/python for more info.

create vercel.json with content:

{
  "rewrites": [{ "source": "/(.*)", "destination": "/api/index" }]
}
vercel deploy

License

sufficient is distributed under the terms of the MIT license.

Project details


Download files

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

Source Distribution

sufficient-0.1.0.tar.gz (479.7 kB view details)

Uploaded Source

Built Distribution

sufficient-0.1.0-py3-none-any.whl (480.3 kB view details)

Uploaded Python 3

File details

Details for the file sufficient-0.1.0.tar.gz.

File metadata

  • Download URL: sufficient-0.1.0.tar.gz
  • Upload date:
  • Size: 479.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.26.0

File hashes

Hashes for sufficient-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fd3121bb254f9782659e273d34ffc0c6049d737f60add79dd2991b4c1261b69a
MD5 4bdd57a42ec5739f14531f0f31cf8967
BLAKE2b-256 b0b861468a943a55ec5191bd6ff3401a743582c70bcd3a1942a8e47a35d54587

See more details on using hashes here.

File details

Details for the file sufficient-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sufficient-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 480.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.26.0

File hashes

Hashes for sufficient-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2b10b5d60dca5154bc0785ea891e6eded2092eaa52a17435580e5041a5f49e1
MD5 eedbac0add27ad92b922a2b9fe167d4d
BLAKE2b-256 399f6a2f9ec1c1491278d510d78690e1cd96e5199db2ba4952eb033fefd028fd

See more details on using hashes here.

Supported by

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