-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:zauberzeug/nicegui
- Loading branch information
Showing
10 changed files
with
119 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Custom Vue Component | ||
|
||
This example demonstrates how to create and use custom Vue components in NiceGUI. | ||
One component is implemented using JavaScript, the other using Vue's Single-File Component (SFC) syntax. | ||
|
||
## Counter Component (counter.js) | ||
|
||
The `Counter` component is a simple counter that increments a value. | ||
On change, it emits an event with the new value. | ||
A reset method allows to reset the counter to 0. | ||
|
||
The JavaScript code in `counter.js` defines the front-end logic of the component using JavaScript. | ||
|
||
## OnOff Component (on_off.vue) | ||
|
||
The `OnOff` component is a simple toggle that switches between on and off. | ||
On change, it emits an event with the new value. | ||
A reset method is also provided to reset the toggle to off. | ||
|
||
The Single-File Component in `on_off.vue` defines the front-end logic of the component using Vue 2. | ||
In contrast to the JavaScript code in `counter.js`, it splits the template, script, and style into separate sections. |
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,17 +1,16 @@ | ||
#!/usr/bin/env python3 | ||
from counter import Counter | ||
from on_off import OnOff | ||
|
||
from nicegui import ui | ||
|
||
ui.markdown(''' | ||
#### Try the new click counter! | ||
with ui.row(align_items='center'): | ||
counter = Counter('Count', on_change=lambda e: ui.notify(f'The value changed to {e.args}.')) | ||
ui.button('Reset', on_click=counter.reset).props('outline') | ||
|
||
Click to increment its value. | ||
''') | ||
with ui.card(): | ||
counter = Counter('Clicks', on_change=lambda e: ui.notify(f'The value changed to {e.args}.')) | ||
with ui.row(align_items='center'): | ||
on_off = OnOff('State', on_change=lambda e: ui.notify(f'The value changed to {e.args}.')) | ||
ui.button('Reset', on_click=on_off.reset).props('outline') | ||
|
||
|
||
ui.button('Reset', on_click=counter.reset).props('small outline') | ||
|
||
ui.run() | ||
ui.run(uvicorn_reload_includes='*.py,*.js,*.vue') |
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,14 @@ | ||
from typing import Callable, Optional | ||
|
||
from nicegui.element import Element | ||
|
||
|
||
class OnOff(Element, component='on_off.vue'): | ||
|
||
def __init__(self, title: str, *, on_change: Optional[Callable] = None) -> None: | ||
super().__init__() | ||
self._props['title'] = title | ||
self.on('change', on_change) | ||
|
||
def reset(self) -> None: | ||
self.run_method('reset') |
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,41 @@ | ||
<template> | ||
<div> | ||
<button @click="handle_click" :class="{ active: value }"> | ||
<strong>{{ title }}: {{ value ? "ON" : "OFF" }}</strong> | ||
</button> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
title: String, | ||
}, | ||
data() { | ||
return { | ||
value: false, | ||
}; | ||
}, | ||
methods: { | ||
handle_click() { | ||
this.value = !this.value; | ||
this.$emit("change", this.value); | ||
}, | ||
reset() { | ||
this.value = false; | ||
}, | ||
}, | ||
}; | ||
</script> | ||
|
||
<style scoped> | ||
button { | ||
background-color: #eee; | ||
padding: 8px 16px; | ||
border-radius: 4px; | ||
} | ||
button.active { | ||
background-color: #fb8; | ||
} | ||
</style> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -196,4 +196,18 @@ async def page(): | |
m.run_map_method('fitBounds', [[bounds['_southWest'], bounds['_northEast']]]) | ||
ui.timer(0, page, once=True) # HIDE | ||
|
||
|
||
@doc.demo('Leaflet Plugins', ''' | ||
You can add plugins to the map by passing the URLs of JS and CSS files to the `additional_resources` parameter. | ||
This demo shows how to add the [Leaflet.RotatedMarker](https://github.com/bbecquet/Leaflet.RotatedMarker) plugin. | ||
It allows you to rotate markers by a given `rotationAngle`. | ||
''') | ||
def leaflet_plugins() -> None: | ||
m = ui.leaflet((51.51, -0.09), additional_resources=[ | ||
'https://unpkg.com/[email protected]/leaflet.rotatedMarker.js', | ||
]) | ||
m.marker(latlng=(51.51, -0.091), options={'rotationAngle': -30}) | ||
m.marker(latlng=(51.51, -0.090), options={'rotationAngle': 30}) | ||
|
||
|
||
doc.reference(ui.leaflet) |