-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.0' into dependabot/pip/fastapi-0.112.2
# Conflicts: # poetry.lock # pyproject.toml
- Loading branch information
Showing
86 changed files
with
1,537 additions
and
804 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
langchain>=0.0.142 | ||
langchain>=0.2 | ||
langchain-community | ||
langchain_openai | ||
nicegui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Use Bundled Node Modules as Third-party Dependencies | ||
|
||
This example demonstrates how to use multiple third-party node modules as dependencies in a NiceGUI app. | ||
The app uses the [is-odd](https://www.npmjs.com/package/is-odd) node modules to check if a number is even or odd. | ||
We chose this package to demonstrate a very simple node module which has a dependency itself, | ||
namely the [is-number](https://www.npmjs.com/package/is-number) package. | ||
Using NPM, we can easily install both packages and bundle them into a single file which can be used in the app. | ||
The package.json file defines the is-odd dependency and some dev dependencies for bundling the node module, | ||
the webpack.config.js file specifies the entry point for the node module, | ||
and number_checker.js as well as number_checker.py define a new UI element to be used in the NiceGUI app main.py. | ||
|
||
1. First, install all third-party node modules (assuming you have NPM installed): | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
This will create a node_modules directory containing the is-odd and is-number modules as well as some dev dependencies. | ||
|
||
2. Now bundle the node module: | ||
|
||
```bash | ||
npm run build | ||
``` | ||
|
||
3. Finally, you can run the app as usual: | ||
|
||
```bash | ||
python3 main.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python3 | ||
from number_checker import NumberChecker | ||
|
||
from nicegui import ui | ||
|
||
|
||
@ui.page('/') | ||
def page(): | ||
number_checker = NumberChecker() | ||
number = ui.number(value=42.0) | ||
|
||
async def check(): | ||
even = await number_checker.is_even(number.value) | ||
ui.notify(f'{number.value} is {"even" if even else "odd"}') | ||
|
||
ui.button('Check', on_click=check) | ||
|
||
|
||
ui.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default { | ||
async mounted() { | ||
await import("is-odd"); | ||
}, | ||
methods: { | ||
isOdd(number) { | ||
return isOdd(number); | ||
}, | ||
isEven(number) { | ||
return !isOdd(number); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from nicegui import ui | ||
|
||
|
||
class NumberChecker(ui.element, component='number_checker.js', dependencies=['dist/is-odd.js']): | ||
|
||
def __init__(self) -> None: | ||
"""NumberChecker | ||
A number checker based on the `is-odd <https://www.npmjs.com/package/is-odd>`_ NPM package. | ||
""" | ||
super().__init__() | ||
|
||
async def is_odd(self, number: int) -> bool: | ||
"""Check if a number is odd.""" | ||
return await self.run_method('isOdd', number) | ||
|
||
async def is_even(self, number: int) -> bool: | ||
"""Check if a number is even.""" | ||
return await self.run_method('isEven', number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"scripts": { | ||
"build": "webpack --config webpack.config.js" | ||
}, | ||
"dependencies": { | ||
"is-odd": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.24.5", | ||
"@babel/preset-env": "^7.24.5", | ||
"babel-loader": "^9.1.3", | ||
"webpack": "^5.91.0", | ||
"webpack-cli": "^5.1.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
entry: "is-odd/index.js", | ||
mode: "development", | ||
output: { | ||
path: path.resolve(__dirname, "dist"), | ||
filename: "is-odd.js", | ||
library: "isOdd", | ||
libraryTarget: "umd", | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: "babel-loader", | ||
options: { | ||
presets: ["@babel/preset-env"], | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use Node Modules as Third-party Dependencies | ||
|
||
This example demonstrates how to use third-party node modules as dependencies in a NiceGUI app. | ||
The app uses the [signature_pad](https://www.npmjs.com/package/signature_pad) node module to create a signature pad. | ||
In package.json, the signature_pad module is listed as a dependency, | ||
while signature_pad.js and signature_pad.py define the new UI element which can be used in main.py. | ||
|
||
1. First, install the third-party node modules (assuming you have NPM installed): | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
This will create a node_modules directory containing the signature_pad module. | ||
|
||
2. Now you can run the app as usual: | ||
|
||
```bash | ||
python3 main.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python3 | ||
from signature_pad import SignaturePad | ||
|
||
from nicegui import ui | ||
|
||
pad = SignaturePad().classes('border') | ||
ui.button('Clear', on_click=pad.clear) | ||
|
||
ui.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"signature_pad": "^4.1.7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import SignaturePad from "signature_pad"; | ||
|
||
export default { | ||
template: "<canvas></canvas>", | ||
props: { | ||
options: Array, | ||
}, | ||
mounted() { | ||
this.pad = new SignaturePad(this.$el, this.options); | ||
}, | ||
methods: { | ||
clear() { | ||
this.pad.clear(); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Dict, Optional | ||
|
||
from nicegui import ui | ||
|
||
|
||
class SignaturePad(ui.element, | ||
component='signature_pad.js', | ||
dependencies=['node_modules/signature_pad/dist/signature_pad.min.js']): | ||
|
||
def __init__(self, options: Optional[Dict] = None) -> None: | ||
"""SignaturePad | ||
An element that integrates the `Signature Pad library <https://szimek.github.io/signature_pad/>`_. | ||
""" | ||
super().__init__() | ||
self._props['options'] = options or {} | ||
|
||
def clear(self): | ||
"""Clear the signature.""" | ||
self.run_method('clear') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import TYPE_CHECKING, Generic, List, Optional, TypeVar | ||
|
||
if TYPE_CHECKING: | ||
from .element import Element | ||
|
||
T = TypeVar('T', bound='Element') | ||
|
||
|
||
class Classes(list, Generic[T]): | ||
|
||
def __init__(self, *args, element: T, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self.element = element | ||
|
||
def __call__(self, | ||
add: Optional[str] = None, *, | ||
remove: Optional[str] = None, | ||
replace: Optional[str] = None) -> T: | ||
"""Apply, remove, or replace HTML classes. | ||
This allows modifying the look of the element or its layout using `Tailwind <https://tailwindcss.com/>`_ or `Quasar <https://quasar.dev/>`_ classes. | ||
Removing or replacing classes can be helpful if predefined classes are not desired. | ||
:param add: whitespace-delimited string of classes | ||
:param remove: whitespace-delimited string of classes to remove from the element | ||
:param replace: whitespace-delimited string of classes to use instead of existing ones | ||
""" | ||
new_classes = self.update_list(self, add, remove, replace) | ||
if self != new_classes: | ||
self[:] = new_classes | ||
self.element.update() | ||
return self.element | ||
|
||
@staticmethod | ||
def update_list(classes: List[str], | ||
add: Optional[str] = None, | ||
remove: Optional[str] = None, | ||
replace: Optional[str] = None) -> List[str]: | ||
"""Update a list of classes.""" | ||
class_list = classes if replace is None else [] | ||
class_list = [c for c in class_list if c not in (remove or '').split()] | ||
class_list += (add or '').split() | ||
class_list += (replace or '').split() | ||
return list(dict.fromkeys(class_list)) # NOTE: remove duplicates while preserving order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.