Skip to content

Commit

Permalink
changed readme
Browse files Browse the repository at this point in the history
  • Loading branch information
anubrag committed Dec 11, 2023
1 parent 7eefc37 commit 1e3ab9e
Showing 1 changed file with 37 additions and 160 deletions.
197 changes: 37 additions & 160 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,185 +41,62 @@ Open a terminal and run (Requires Python 3.7+):
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.
## ⚙️ Installation

You can run this app in development mode:
Open a terminal and run (Requires Python 3.7+):

```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.

 



 

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()
pip install nextpy
```
## 🚀 Quick Start with Nextpy

## Let's break this down.
Here's a simplified guide to get your first Nextpy app up and running:

### **Nextpy UI**
### Setting Up

Now let's explore the main components of the DALL·E image generation app.
1. **Make a New Directory:**
Open your terminal and create a directory for your app:

### **The index Function**
```bash
mkdir my_nextpy_app
```

```python
def index():
return xt.center(
...
)
```
2. **Go to Your New Directory:**
Change to your new app's directory:
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.
```bash
cd my_nextpy_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.
3. **Start Your Nextpy App:**
Run the following to initialize your app:
```bash
nextpy init
```
### **State**
Choose from:
- **Blank Template**: Start from scratch.
- **Base Template**: Use a basic pre-setup.
Nextpy represents your UI as a function of your state.
### Running Your App
```python
class State(xt.State):
"""The app state."""
prompt = ""
image_url = ""
processing = False
complete = False
```
1. **Run the App:**
Inside your app's directory, type:

The State class defines the reactive variables, or "vars," that store the application's data and state. In this example, we have:
```bash
nextpy run
```

- 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.
2. **See Your App:**
Open [http://localhost:3000](http://localhost:3000) in a browser.

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.
Enjoy building with Nextpy!

## 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
```
![-----------------------------------------------------](https://res.cloudinary.com/dzznkbdrb/image/upload/v1694798498/divider_1_rej288.gif)

## 🙏 Thanks

## 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.
Nextpy Framework is a state-of-the-art tool for AI-based code generation, built on the open-source community’s spirit of cooperation. It integrates key components from landmark projects like Guidance, Llama-Index, FastAPI-Mail, LangChain, ReactPy, Reflex, Chakra, Radix, Numpy and Next.js, while also drawing insights from the React and Rust ecosystems. This fusion ideas has been pivotal in shaping Nextpy into a framework that's not just AI-friendly but also a trailblazer in generative web development tools.
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.
We are deeply grateful to the open-source creators, contributors, and maintainers whose work has provided the basis for Nextpy. Your commitment to innovation and openness has been vital for shaping this framework. Your contributions have not only enhanced Nextpy but are also advancing the new era of AI-powered software development. Thank you for being the catalysts and enablers of this transformational journey.

0 comments on commit 1e3ab9e

Please sign in to comment.