ReactPy Documentation

repository·main·Indexed 27 days ago

https://github.com/reactive-python/reactpy

A Python library for building reactive user interfaces using a component-based model inspired by ReactJS, eliminating the need for JavaScript development. It features built-in support for Flask, FastAPI, Sanic, and Tornado, with external integrations for Django, Jupyter, and Plotly-Dash. The library includes state management via the use_state hook and a React-based client implementation (@reactpy/client).

Tokens
17K
Snippets
40
Records
136
Agent score
92%

What's inside ReactPy

  1. Overview of ReactPy concepts

    main

    ReactPy is a library for building user interfaces in Python without needing JavaScript. It uses a component-based architecture similar to ReactJS. Key concepts include:

    • Components: Reusable UI elements created using the @component decorator.
    • Hooks: Special functions (starting with use, such as use_state()) that allow components to 'hook into' ReactPy capabilities like state management. Hooks must only be called during the rendering process.
    • State: Managed via hooks like use_state(), which provides a state variable and a state setter to allow components to 'remember' information (like input values or shopping carts) and respond to user interactions.
  2. Introduction to ReactPy

    main
    ReactPy is a library for building user interfaces in Python without writing JavaScript. It uses a component-based model similar to ReactJS, allowing developers to create reactive UIs using pure Python. It is designed to be accessible to those without web development experience while remaining powerful enough for complex applications.
  3. Run ReactPy in production with various backends

    main

    To run ReactPy in production, you must choose a backend implementation and configure it with a ReactPy view. The general pattern is to initialize your chosen backend's Application and then use the configure function from that backend to attach your component.

    Example pattern:

    from my_chosen_backend import Application
    from reactpy import component, html
    from reactpy.backend.my_chosen_backend import configure
    
    @component
    def HelloWorld():
        return html.h1("Hello, world!")
    
    app = Application()
    configure(app, HelloWorld)

    You then run the resulting app using an ASGI or WSGI server from the command line.

    from my_chosen_backend import Application
    
    from reactpy import component, html
    from reactpy.backend.my_chosen_backend import configure
    
    
    @component
    def HelloWorld():
        return html.h1("Hello, world!")
    
    
    app = Application()
    configure(app, HelloWorld)
  4. Create a Hello World application in ReactPy

    main
    To build a basic ReactPy application, define an App function that returns HTML elements (like h1). Apply the @component decorator to the function to turn it into a ReactPy component, and then use the run() function to start a development web server with that component.
  5. Add event handlers to components

    main

    To make UI elements interactive, assign a function to an event attribute (e.g., "onClick") within an element's properties dictionary. You can define handlers inside your component to access local scope, or use lambda functions for simple logic.

    Note: print() statements in handlers will appear in the terminal where the ReactPy server is running, not in the browser console.

  6. Install the flake8-reactpy-hooks linter plugin

    main

    To automatically enforce the Rules of Hooks in your development workflow, you can install the flake8-reactpy-hooks plugin. This plugin will catch common mistakes like using hooks inside conditional statements or calling them outside of component/hook definitions when running flake8.

    pip install flake8-reactpy-hooks