toolhost


Nametoolhost 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-13 19:16:41
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
from openai import OpenAI
client = OpenAI()


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 = client.images.generate(
            model="dall-e-3",
            prompt="a white siamese cat",
            size="1024x1024",
            quality="standard",
            n=1,
            )
        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**

Now let's explore the main components of the DALL路E image generation app.

### **The index Function**

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

This `index` function serves as the main entry point for defining the structure and content of the app's front-end user interface. This function utilizes Nextpy's declarative UI components to specify what the app should look like. When the app runs, whatever is returned by the index function is rendered on the screen.

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 class defines the reactive variables, or "vars," that store the application's data and state. In this example, we have:

- prompt: A string to capture user input for image generation.
- image_url: A string to store the URL of the generated image.
- processing: A boolean indicating whether the image generation is in progress.
- complete: A boolean that turns true when image processing is finished.

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.

## State
```python
class State(xt.State):
    prompt = ""
    image_url = ""
    processing = False
    complete = False
```
The State class defines the reactive variables, or "vars," that store the application's data and state. In this example, we have:

- `prompt`: A string to capture user input for image generation.
- `image_url`: A string to store the URL of the generated image.
- `processing`: A boolean indicating whether the image generation is in progress.
- `complete`: A boolean that turns true when image processing is finished.

Furthermore, the `State` class also contains event handlers that react to user-expressed actions, altering the state as required. The `get_image` event handler initiates image generation when a user submits a prompt, using the `yield` statement to refresh the UI whenever needed.

```python
def get_image(self):
    if self.prompt == "":
        return xt.window_alert("Prompt Empty")
    self.processing, self.complete = True, False
    yield
    response = client.images.generate(...)
    self.image_url = ...  # Extract image URL from response
    self.processing, self.complete = False, True
```


## App Configuration and Routing
```python
app = xt.App()
app.add_page(index, title="nextpy:DALL路E")
app.compile()
```
The app's configuration is established with an instance of `xt.App` which encapsulates the entire Nextpy application. The `index` function is added as the root page, and the `app.compile()` call prepares the app for execution.

This breakdown demonstrates how to structure a Nextpy application, implement state management, define event handlers, and construct a responsive UI, culminating in a deployable web application.

            

Raw data

            {
    "_id": null,
    "home_page": "https://dotagent.dev",
    "name": "toolhost",
    "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/6d/d1/cfee58100696507fd34e373b58bc603b0019633c984c9e26ea2a2a9af526/toolhost-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\nfrom openai import OpenAI\nclient = OpenAI()\n\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 = client.images.generate(\n            model=\"dall-e-3\",\n            prompt=\"a white siamese cat\",\n            size=\"1024x1024\",\n            quality=\"standard\",\n            n=1,\n            )\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\nNow let's explore the main components of the DALL\u00b7E image generation app.\n\n### **The index Function**\n\n```python\ndef index():\n    return xt.center(\n        ...\n    )\n```\n\nThis `index` function serves as the main entry point for defining the structure and content of the app's front-end user interface. This function utilizes Nextpy's declarative UI components to specify what the app should look like. When the app runs, whatever is returned by the index function is rendered on the screen.\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 class defines the reactive variables, or \"vars,\" that store the application's data and state. In this example, we have:\n\n- prompt: A string to capture user input for image generation.\n- image_url: A string to store the URL of the generated image.\n- processing: A boolean indicating whether the image generation is in progress.\n- complete: A boolean that turns true when image processing is finished.\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## State\n```python\nclass State(xt.State):\n    prompt = \"\"\n    image_url = \"\"\n    processing = False\n    complete = False\n```\nThe State class defines the reactive variables, or \"vars,\" that store the application's data and state. In this example, we have:\n\n- `prompt`: A string to capture user input for image generation.\n- `image_url`: A string to store the URL of the generated image.\n- `processing`: A boolean indicating whether the image generation is in progress.\n- `complete`: A boolean that turns true when image processing is finished.\n\nFurthermore, the `State` class also contains event handlers that react to user-expressed actions, altering the state as required. The `get_image` event handler initiates image generation when a user submits a prompt, using the `yield` statement to refresh the UI whenever needed.\n\n```python\ndef get_image(self):\n    if self.prompt == \"\":\n        return xt.window_alert(\"Prompt Empty\")\n    self.processing, self.complete = True, False\n    yield\n    response = client.images.generate(...)\n    self.image_url = ...  # Extract image URL from response\n    self.processing, self.complete = False, True\n```\n\n\n## App Configuration and Routing\n```python\napp = xt.App()\napp.add_page(index, title=\"nextpy:DALL\u00b7E\")\napp.compile()\n```\nThe app's configuration is established with an instance of `xt.App` which encapsulates the entire Nextpy application. The `index` function is added as the root page, and the `app.compile()` call prepares the app for execution.\n\nThis breakdown demonstrates how to structure a Nextpy application, implement state management, define event handlers, and construct a responsive UI, culminating in a deployable web application.\n",
    "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": "00a7e08a272df795de5088b203c905f0fd91fc98847a917626c9122ee74a6b1b",
                "md5": "6f0cce64bc96a5633a72ac3f7d6258d1",
                "sha256": "f96c24a3c133af1e358579cb9c43b6fb33075b487203cda0a1fc3e45dcac111c"
            },
            "downloads": -1,
            "filename": "toolhost-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6f0cce64bc96a5633a72ac3f7d6258d1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 479780,
            "upload_time": "2023-11-13T19:16:36",
            "upload_time_iso_8601": "2023-11-13T19:16:36.319355Z",
            "url": "https://files.pythonhosted.org/packages/00/a7/e08a272df795de5088b203c905f0fd91fc98847a917626c9122ee74a6b1b/toolhost-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6dd1cfee58100696507fd34e373b58bc603b0019633c984c9e26ea2a2a9af526",
                "md5": "8f7240d6a45c5304d4fe344563c604a3",
                "sha256": "b1f29ba3611ee0677e96d78077eaf26950c4f93a609eb95c56e9fb3ad6a8b070"
            },
            "downloads": -1,
            "filename": "toolhost-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "8f7240d6a45c5304d4fe344563c604a3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 304373,
            "upload_time": "2023-11-13T19:16:41",
            "upload_time_iso_8601": "2023-11-13T19:16:41.067460Z",
            "url": "https://files.pythonhosted.org/packages/6d/d1/cfee58100696507fd34e373b58bc603b0019633c984c9e26ea2a2a9af526/toolhost-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-13 19:16:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dot-agent",
    "github_project": "nextpy",
    "github_not_found": true,
    "lcname": "toolhost"
}
        
Elapsed time: 0.13918s