Lost little book boy
Needed after reading
5
minuteSpeed reading only takes 2 minutes
1
Introduction
While there are many Python web application frameworks out there, most are designed for small data applications or use paradigms that have not been proven at scale. As application complexity increases, code organization, reusability, and state often suffer, and the result is either cluttered code or a direct use of React to develop applications.
Solara solves this problem. With a React-like API, we don't need to worry about scalability. React has proven its ability to power the world's largest web applications. Solara uses a pure Python implementation of React (Reacton) to create ipywidget-based applications. These applications can run either within Jupyter Notebook or as standalone web applications through frameworks such as FastAPI. This paradigm enables component-based code and extremely simple state management.
By building on top of ipywidgets, we can automatically leverage the existing widget ecosystem and run on many platforms, including JupyterLab, Jupyter Notebook, Voilà, Google Colab, DataBricks, JetBrains Datalore, and more.
2
Install
Install using pip command
pip install solara
3
basic grammar
3.1
Create reactive variables
Use solara.reactive() to create responsive variables, which can be bound to UI components to implement responsive updates of data.
count = solara.reactive(0)
3.2
Define components
Components can be defined using the @solara.component decorator
@solara.component
def MyComponent():
# 组件的实现
Components can display responsive variables or contain other solara components
3.3
Reference component
Directly call the component function to render the component
MyComponent()
3.4
Update reactive variables
If a reactive variable is updated, the component it is bound to will also be automatically updated.
count.value += 1
solara connects data and components through responsive variables, and uses simple Python syntax to build dynamic pages
4
Sample code
The following is an official complete code example
import solara
sentence = solara.reactive("Solara makes our team more productive.")
word_limit = solara.reactive(10)
@solara.component
def Page():
word_count = len(sentence.value.split())
solara.SliderInt("Word limit", value=word_limit, min=2, max=20)
solara.InputText(label="Your sentence", value=sentence, continuous_update=True)
if word_count >= int(word_limit.value):
solara.Error(f"With {word_count} words, you passed the word limit of {word_limit.value}.")
elif word_count >= int(0.8 * word_limit.value):
solara.Warning(f"With {word_count} words, you are close to the word limit of {word_limit.value}.")
else:
solara.Success("Great short writing!")
Page()
To execute the above code, the command is
solara run sol.py
A web service will be started and http://localhost:8765 will be automatically opened using the default browser.
This code uses the solara library to implement a simple page:
Import the solara library and create a responsive sentence variable sentence and word limit variable word_limit. Then, define a Page component using the solara.component decorator. Inside the component, count the number of words in the sentence word_count. Create a slider component using solara.SliderInt and bind it to the word_limit variable. Use solara.InputText to create a text input component and bind it to the sentence variable. According to the relationship between word_count and word_limit, different printing information is displayed. If it exceeds the limit, an Error prompt is displayed; if it is close to the limit, a Warning prompt is displayed, otherwise a Success message is displayed. Finally, the Page component is called for rendering. In this way, we can modify the limit in real time by dragging the slider, and enter a sentence to see the change of the word count prompt.
5
References
https://github.com/widgetti/solara
6
free community