openagent-dev


Nameopenagent-dev JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://dotagent.dev
SummaryWeb apps in pure Python with all the flexibility and speed of nextjs.
upload_time2023-11-12 09:35:28
maintainer
docs_urlNone
authorTeam dotagent
requires_python>=3.8,<4.0
licenseApache-2.0
keywords web framework
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
## ⚙️ Installation

Open a terminal and run (Requires Python 3.7+):

```bash
pip install nextpy
```

## 🥳 Create your first app

Installing `nextpy` also installs the `nextpy` command line tool.

Test that the install was successful by creating a new project. (Replace `my_app_name` with your project name):

```bash
mkdir my_app_name
cd my_app_name
nextpy init
```

This command initializes a boilerplate app in your new directory. 

You can run this app in development mode:

```bash
nextpy run
```

You should see your app running at http://localhost:3000.

Now you can modify the source code in `my_app_name/my_app_name.py`. Nextpy has fast refreshes so you can see your changes instantly when you save your code.


## 🫧 Example App

Let's go over an example: creating an image generation UI around DALL·E. For simplicity, we just call the OpenAI API, but you could replace this with an ML model run locally.

&nbsp;



&nbsp;

Here is the complete code to create this. This is all done in one Python file!

```python
import nextpy as xt
import openai

openai.api_key = "YOUR_API_KEY"

class State(xt.State):
    """The app state."""
    prompt = ""
    image_url = ""
    processing = False
    complete = False

    def get_image(self):
        """Get the image from the prompt."""
        if self.prompt == "":
            return xt.window_alert("Prompt Empty")

        self.processing, self.complete = True, False
        yield
        response = openai.Image.create(prompt=self.prompt, n=1, size="1024x1024")
        self.image_url = response["data"][0]["url"]
        self.processing, self.complete = False, True
        

def index():
    return xt.center(
        xt.vstack(
            xt.heading("DALL·E"),
            xt.input(placeholder="Enter a prompt", on_blur=State.set_prompt),
            xt.button(
                "Generate Image",
                on_click=State.get_image,
                is_loading=State.processing,
                width="100%",
            ),
            xt.cond(
                State.complete,
                     xt.image(
                         src=State.image_url,
                         height="25em",
                         width="25em",
                    )
            ),
            padding="2em",
            shadow="lg",
            border_radius="lg",
        ),
        width="100%",
        height="100vh",
    )

# Add state and page to the app.
app = xt.App()
app.add_page(index, title="nextpy:DALL·E")
app.compile()
```

## Let's break this down.

### **Nextpy UI**

Let's start with the UI.

```python
def index():
    return xt.center(
        ...
    )
```

This `index` function defines the frontend of the app.

We use different components such as `center`, `vstack`, `input`, and `button` to build the frontend. Components can be nested within each other
to create complex layouts. And you can use keyword args to style them with the full power of CSS.


### **State**

Nextpy represents your UI as a function of your state.

```python
class State(xt.State):
    """The app state."""
    prompt = ""
    image_url = ""
    processing = False
    complete = False
```

The state defines all the variables (called vars) in an app that can change and the functions that change them.

Here the state is comprised of a `prompt` and `image_url`. There are also the booleans `processing` and `complete` to indicate when to show the circular progress and image.

### **Event Handlers**

```python
def get_image(self):
    """Get the image from the prompt."""
    if self.prompt == "":
        return xt.window_alert("Prompt Empty")

    self.processing, self.complete = True, False
    yield
    response = openai.Image.create(prompt=self.prompt, n=1, size="1024x1024")
    self.image_url = response["data"][0]["url"]
    self.processing, self.complete = False, True
```

Within the state, we define functions called event handlers that change the state vars. Event handlers are the way that we can modify the state in Nextpy. They can be called in response to user actions, such as clicking a button or typing in a text box. These actions are called events.

Our DALL·E. app has an event handler, `get_image` to which get this image from the OpenAI API. Using `yield` in the middle of an event handler will cause the UI to update. Otherwise the UI will update at the end of the event handler.

### **Routing**

Finally, we define our app.

```python
app = xt.App()
```

We add a page from the root of the app to the index component. We also add a title that will show up in the page preview/browser tab.

```python
app.add_page(index, title="DALL-E")
app.compile()
```

You can create a multi-page app by adding more pages.
            

Raw data

            {
    "_id": null,
    "home_page": "https://dotagent.dev",
    "name": "openagent-dev",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "web,framework",
    "author": "Team dotagent",
    "author_email": "anurag@dotagent.ai",
    "download_url": "https://files.pythonhosted.org/packages/d0/28/52baf1b6140be175573b9f75f41b3d5b3c27b6365be7b43e2a56167ecee7/openagent_dev-0.2.1.tar.gz",
    "platform": null,
    "description": "\n## \u2699\ufe0f Installation\n\nOpen a terminal and run (Requires Python 3.7+):\n\n```bash\npip install nextpy\n```\n\n## \ud83e\udd73 Create your first app\n\nInstalling `nextpy` also installs the `nextpy` command line tool.\n\nTest that the install was successful by creating a new project. (Replace `my_app_name` with your project name):\n\n```bash\nmkdir my_app_name\ncd my_app_name\nnextpy init\n```\n\nThis command initializes a boilerplate app in your new directory. \n\nYou can run this app in development mode:\n\n```bash\nnextpy run\n```\n\nYou should see your app running at http://localhost:3000.\n\nNow you can modify the source code in `my_app_name/my_app_name.py`. Nextpy has fast refreshes so you can see your changes instantly when you save your code.\n\n\n## \ud83e\udee7 Example App\n\nLet's go over an example: creating an image generation UI around DALL\u00b7E. For simplicity, we just call the OpenAI API, but you could replace this with an ML model run locally.\n\n&nbsp;\n\n\n\n&nbsp;\n\nHere is the complete code to create this. This is all done in one Python file!\n\n```python\nimport nextpy as xt\nimport openai\n\nopenai.api_key = \"YOUR_API_KEY\"\n\nclass State(xt.State):\n    \"\"\"The app state.\"\"\"\n    prompt = \"\"\n    image_url = \"\"\n    processing = False\n    complete = False\n\n    def get_image(self):\n        \"\"\"Get the image from the prompt.\"\"\"\n        if self.prompt == \"\":\n            return xt.window_alert(\"Prompt Empty\")\n\n        self.processing, self.complete = True, False\n        yield\n        response = openai.Image.create(prompt=self.prompt, n=1, size=\"1024x1024\")\n        self.image_url = response[\"data\"][0][\"url\"]\n        self.processing, self.complete = False, True\n        \n\ndef index():\n    return xt.center(\n        xt.vstack(\n            xt.heading(\"DALL\u00b7E\"),\n            xt.input(placeholder=\"Enter a prompt\", on_blur=State.set_prompt),\n            xt.button(\n                \"Generate Image\",\n                on_click=State.get_image,\n                is_loading=State.processing,\n                width=\"100%\",\n            ),\n            xt.cond(\n                State.complete,\n                     xt.image(\n                         src=State.image_url,\n                         height=\"25em\",\n                         width=\"25em\",\n                    )\n            ),\n            padding=\"2em\",\n            shadow=\"lg\",\n            border_radius=\"lg\",\n        ),\n        width=\"100%\",\n        height=\"100vh\",\n    )\n\n# Add state and page to the app.\napp = xt.App()\napp.add_page(index, title=\"nextpy:DALL\u00b7E\")\napp.compile()\n```\n\n## Let's break this down.\n\n### **Nextpy UI**\n\nLet's start with the UI.\n\n```python\ndef index():\n    return xt.center(\n        ...\n    )\n```\n\nThis `index` function defines the frontend of the app.\n\nWe use different components such as `center`, `vstack`, `input`, and `button` to build the frontend. Components can be nested within each other\nto create complex layouts. And you can use keyword args to style them with the full power of CSS.\n\n\n### **State**\n\nNextpy represents your UI as a function of your state.\n\n```python\nclass State(xt.State):\n    \"\"\"The app state.\"\"\"\n    prompt = \"\"\n    image_url = \"\"\n    processing = False\n    complete = False\n```\n\nThe state defines all the variables (called vars) in an app that can change and the functions that change them.\n\nHere the state is comprised of a `prompt` and `image_url`. There are also the booleans `processing` and `complete` to indicate when to show the circular progress and image.\n\n### **Event Handlers**\n\n```python\ndef get_image(self):\n    \"\"\"Get the image from the prompt.\"\"\"\n    if self.prompt == \"\":\n        return xt.window_alert(\"Prompt Empty\")\n\n    self.processing, self.complete = True, False\n    yield\n    response = openai.Image.create(prompt=self.prompt, n=1, size=\"1024x1024\")\n    self.image_url = response[\"data\"][0][\"url\"]\n    self.processing, self.complete = False, True\n```\n\nWithin the state, we define functions called event handlers that change the state vars. Event handlers are the way that we can modify the state in Nextpy. They can be called in response to user actions, such as clicking a button or typing in a text box. These actions are called events.\n\nOur DALL\u00b7E. app has an event handler, `get_image` to which get this image from the OpenAI API. Using `yield` in the middle of an event handler will cause the UI to update. Otherwise the UI will update at the end of the event handler.\n\n### **Routing**\n\nFinally, we define our app.\n\n```python\napp = xt.App()\n```\n\nWe add a page from the root of the app to the index component. We also add a title that will show up in the page preview/browser tab.\n\n```python\napp.add_page(index, title=\"DALL-E\")\napp.compile()\n```\n\nYou can create a multi-page app by adding more pages.",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Web apps in pure Python with all the flexibility and speed of nextjs.",
    "version": "0.2.1",
    "project_urls": {
        "Documentation": "https://dotagent.dev/docs/getting-started/introduction",
        "Homepage": "https://dotagent.dev",
        "Repository": "https://github.com/dot-agent/nextpy"
    },
    "split_keywords": [
        "web",
        "framework"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a17f88567e3ac080fb220998045bd3cdb5853f664867d01ee48dd018c1b1323c",
                "md5": "114d4812b5fc9f9d9e53cabe14b001eb",
                "sha256": "48a3c75df13a56d370a99f86c6ce3c63afc55aafa6c16bbd1442b04a95990150"
            },
            "downloads": -1,
            "filename": "openagent_dev-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "114d4812b5fc9f9d9e53cabe14b001eb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 479491,
            "upload_time": "2023-11-12T09:35:24",
            "upload_time_iso_8601": "2023-11-12T09:35:24.210127Z",
            "url": "https://files.pythonhosted.org/packages/a1/7f/88567e3ac080fb220998045bd3cdb5853f664867d01ee48dd018c1b1323c/openagent_dev-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d02852baf1b6140be175573b9f75f41b3d5b3c27b6365be7b43e2a56167ecee7",
                "md5": "e3caa264362e642377661e88bf036729",
                "sha256": "35ddf689634b7f9d0108096f47de2a4690efbf54366b1ab9d94545a33ddadce1"
            },
            "downloads": -1,
            "filename": "openagent_dev-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e3caa264362e642377661e88bf036729",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 303547,
            "upload_time": "2023-11-12T09:35:28",
            "upload_time_iso_8601": "2023-11-12T09:35:28.098656Z",
            "url": "https://files.pythonhosted.org/packages/d0/28/52baf1b6140be175573b9f75f41b3d5b3c27b6365be7b43e2a56167ecee7/openagent_dev-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-12 09:35:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dot-agent",
    "github_project": "nextpy",
    "github_not_found": true,
    "lcname": "openagent-dev"
}
        
Elapsed time: 0.14750s