Package: toolhost
⚙️ InstallationOpen a terminal and run (Requires Python 3.7+):pipinstallnextpy🥳 Create your first appInstallingnextpyalso installs thenextpycommand line tool.Test that the install was successful by creating a new project. (Replacemy_app_namewith your project name):mkdirmy_app_namecdmy_app_name
nextpyinitThis command initializes a boilerplate app in your new directory.You can run this app in development mode:nextpyrunYou should see your app running athttp://localhost:3000.Now you can modify the source code inmy_app_name/my_app_name.py. Nextpy has fast refreshes so you can see your changes instantly when you save your code.🫧 Example AppLet'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!importnextpyasxtfromopenaiimportOpenAIclient=OpenAI()classState(xt.State):"""The app state."""prompt=""image_url=""processing=Falsecomplete=Falsedefget_image(self):"""Get the image from the prompt."""ifself.prompt=="":returnxt.window_alert("Prompt Empty")self.processing,self.complete=True,Falseyieldresponse=client.images.generate(model="dall-e-3",prompt="a white siamese cat",size="1024x1024",quality="standard",n=1,)self.image_url=response.data[0].urlself.processing,self.complete=False,Truedefindex():returnxt.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 UINow let's explore the main components of the DALL·E image generation app.The index Functiondefindex():returnxt.center(...)Thisindexfunction 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 ascenter,vstack,input, andbuttonto 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.StateNextpy represents your UI as a function of your state.classState(xt.State):"""The app state."""prompt=""image_url=""processing=Falsecomplete=FalseThe 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 apromptandimage_url. There are also the booleansprocessingandcompleteto indicate when to show the circular progress and image.StateclassState(xt.State):prompt=""image_url=""processing=Falsecomplete=FalseThe 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, theStateclass also contains event handlers that react to user-expressed actions, altering the state as required. Theget_imageevent handler initiates image generation when a user submits a prompt, using theyieldstatement to refresh the UI whenever needed.defget_image(self):ifself.prompt=="":returnxt.window_alert("Prompt Empty")self.processing,self.complete=True,Falseyieldresponse=client.images.generate(...)self.image_url=...# Extract image URL from responseself.processing,self.complete=False,TrueApp Configuration and Routingapp=xt.App()app.add_page(index,title="nextpy:DALL·E")app.compile()The app's configuration is established with an instance ofxt.Appwhich encapsulates the entire Nextpy application. Theindexfunction is added as the root page, and theapp.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.