dotreact


Namedotreact JSON
Version 0.1.3 PyPI version JSON
download
home_pagehttps://dotagent.dev
SummaryWeb apps in pure Python.
upload_time2023-10-07 18:27:28
maintainer
docs_urlNone
authorTeam dotagent
requires_python>=3.7,<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 dotreact
```

## 🥳 Create your first app

Installing `dotreact` also installs the `dotreact` 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
dotreact init
```

This command initializes a template app in your new directory. 

You can run this app in development mode:

```bash
dotreact 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`. Dotreact 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;

<div align="center">
<img src="https://raw.githubusercontent.com/dotreact/dotreact/main/docs/images/dalle.gif" alt="A frontend wrapper for DALL·E, shown in the process of generating an image." width="550" />
</div>

&nbsp;

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

```python
import dotreact as dr
import openai

openai.api_key = "YOUR_API_KEY"

class State(dr.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 dr.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 dr.center(
        dr.vstack(
            dr.heading("DALL·E"),
            dr.input(placeholder="Enter a prompt", on_blur=State.set_prompt),
            dr.button(
                "Generate Image",
                on_click=State.get_image,
                is_loading=State.processing,
                width="100%",
            ),
            dr.cond(
                State.complete,
                     dr.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 = dr.App()
app.add_page(index, title="dotreact:DALL·E")
app.compile()
```

## Let's break this down.

### **Dotreact UI**

Let's start with the UI.

```python
def index():
    return dr.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.

Dotreact comes with [60+ built-in components](https://dotagent.dev/docs/library) to help you get started. We are actively adding more components, and it's easy to [create your own components](https://dotagent.dev/docs/advanced-guide/wrapping-react).

### **State**

Dotreact represents your UI as a function of your state.

```python
class State(dr.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 dr.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 Dotreact. 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 = dr.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.

## 📑 Resources

<div align="center">

📑 [Docs](https://dotagent.dev/docs/getting-started/introduction) &nbsp; |  &nbsp; 🗞️ [Blog](https://dotagent.dev/blog) &nbsp; |  &nbsp; 📱 [Component Library](https://dotagent.dev/docs/library) &nbsp; |  &nbsp; 🖼️ [Gallery](https://dotagent.dev/docs/gallery) &nbsp; |  &nbsp; 🛸 [Deployment](https://dotagent.dev/docs/hosting/deploy)  &nbsp;   

</div>





## ✅ Status

Dotreact launched in December 2022 with the name Pynecone.

As of July 2023, we are in the **Public Beta** stage.

-   :white_check_mark: **Public Alpha**: Anyone can install and use Dotreact. There may be issues, but we are working to resolve them actively.
-   :large_orange_diamond: **Public Beta**: Stable enough for non-enterprise use-cases.
-   **Public Hosting Beta**: _Optionally_, deploy and host your apps on Dotreact!
-   **Public**: Dotreact is production ready.

Dotreact has new releases and features coming every week! Make sure to :star: star and :eyes: watch this repository to stay up to date.


We are actively looking for contributors, no matter your skill level or experience.

## License

Dotreact is open-source and licensed under the [Apache License 2.0](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": "https://dotagent.dev",
    "name": "dotreact",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "web,framework",
    "author": "Team dotagent",
    "author_email": "anurag@dotagent.dev",
    "download_url": "https://files.pythonhosted.org/packages/2f/da/dfbf196fc10414e501697e9fb19988a827b62f6ef6b847e9e5a1b32adc95/dotreact-0.1.3.tar.gz",
    "platform": null,
    "description": "\n## \u2699\ufe0f Installation\n\nOpen a terminal and run (Requires Python 3.7+):\n\n```bash\npip install dotreact\n```\n\n## \ud83e\udd73 Create your first app\n\nInstalling `dotreact` also installs the `dotreact` 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\ndotreact init\n```\n\nThis command initializes a template app in your new directory. \n\nYou can run this app in development mode:\n\n```bash\ndotreact 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`. Dotreact 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<div align=\"center\">\n<img src=\"https://raw.githubusercontent.com/dotreact/dotreact/main/docs/images/dalle.gif\" alt=\"A frontend wrapper for DALL\u00b7E, shown in the process of generating an image.\" width=\"550\" />\n</div>\n\n&nbsp;\n\nHere is the complete code to create this. This is all done in one Python file!\n\n```python\nimport dotreact as dr\nimport openai\n\nopenai.api_key = \"YOUR_API_KEY\"\n\nclass State(dr.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 dr.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 dr.center(\n        dr.vstack(\n            dr.heading(\"DALL\u00b7E\"),\n            dr.input(placeholder=\"Enter a prompt\", on_blur=State.set_prompt),\n            dr.button(\n                \"Generate Image\",\n                on_click=State.get_image,\n                is_loading=State.processing,\n                width=\"100%\",\n            ),\n            dr.cond(\n                State.complete,\n                     dr.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 = dr.App()\napp.add_page(index, title=\"dotreact:DALL\u00b7E\")\napp.compile()\n```\n\n## Let's break this down.\n\n### **Dotreact UI**\n\nLet's start with the UI.\n\n```python\ndef index():\n    return dr.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\nDotreact comes with [60+ built-in components](https://dotagent.dev/docs/library) to help you get started. We are actively adding more components, and it's easy to [create your own components](https://dotagent.dev/docs/advanced-guide/wrapping-react).\n\n### **State**\n\nDotreact represents your UI as a function of your state.\n\n```python\nclass State(dr.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 dr.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 Dotreact. 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 = dr.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.\n\n## \ud83d\udcd1 Resources\n\n<div align=\"center\">\n\n\ud83d\udcd1 [Docs](https://dotagent.dev/docs/getting-started/introduction) &nbsp; |  &nbsp; \ud83d\uddde\ufe0f [Blog](https://dotagent.dev/blog) &nbsp; |  &nbsp; \ud83d\udcf1 [Component Library](https://dotagent.dev/docs/library) &nbsp; |  &nbsp; \ud83d\uddbc\ufe0f [Gallery](https://dotagent.dev/docs/gallery) &nbsp; |  &nbsp; \ud83d\udef8 [Deployment](https://dotagent.dev/docs/hosting/deploy)  &nbsp;   \n\n</div>\n\n\n\n\n\n## \u2705 Status\n\nDotreact launched in December 2022 with the name Pynecone.\n\nAs of July 2023, we are in the **Public Beta** stage.\n\n-   :white_check_mark: **Public Alpha**: Anyone can install and use Dotreact. There may be issues, but we are working to resolve them actively.\n-   :large_orange_diamond: **Public Beta**: Stable enough for non-enterprise use-cases.\n-   **Public Hosting Beta**: _Optionally_, deploy and host your apps on Dotreact!\n-   **Public**: Dotreact is production ready.\n\nDotreact has new releases and features coming every week! Make sure to :star: star and :eyes: watch this repository to stay up to date.\n\n\nWe are actively looking for contributors, no matter your skill level or experience.\n\n## License\n\nDotreact is open-source and licensed under the [Apache License 2.0](LICENSE).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Web apps in pure Python.",
    "version": "0.1.3",
    "project_urls": {
        "Documentation": "https://dotagent.dev/docs/getting-started/introduction",
        "Homepage": "https://dotagent.dev",
        "Repository": "https://github.com/dot-agent/dotreact"
    },
    "split_keywords": [
        "web",
        "framework"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c3da33cc646155e7004a6e747e8a59f703442b6152653cf18b3110d50e638043",
                "md5": "834bd499db84fcdb97d75cce1338952d",
                "sha256": "35e7b5814bb0ff146cad08ddb483739a2acd8b639ddbb5efbbce5e34168827a6"
            },
            "downloads": -1,
            "filename": "dotreact-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "834bd499db84fcdb97d75cce1338952d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 358671,
            "upload_time": "2023-10-07T18:27:26",
            "upload_time_iso_8601": "2023-10-07T18:27:26.803752Z",
            "url": "https://files.pythonhosted.org/packages/c3/da/33cc646155e7004a6e747e8a59f703442b6152653cf18b3110d50e638043/dotreact-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2fdadfbf196fc10414e501697e9fb19988a827b62f6ef6b847e9e5a1b32adc95",
                "md5": "a78c57bffa18f6420949d9742d718bf2",
                "sha256": "c1c5e53b9a07c63236bd582a997a248b9c728bddd5590d020573cafb92a4f1fa"
            },
            "downloads": -1,
            "filename": "dotreact-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "a78c57bffa18f6420949d9742d718bf2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 224296,
            "upload_time": "2023-10-07T18:27:28",
            "upload_time_iso_8601": "2023-10-07T18:27:28.784452Z",
            "url": "https://files.pythonhosted.org/packages/2f/da/dfbf196fc10414e501697e9fb19988a827b62f6ef6b847e9e5a1b32adc95/dotreact-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-07 18:27:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dot-agent",
    "github_project": "dotreact",
    "github_not_found": true,
    "lcname": "dotreact"
}
        
Elapsed time: 0.21299s