Skip to content

Commit 8fd47d7

Browse files
committed
Add an example of using type hint to convert string to float.
1 parent 5bb6da5 commit 8fd47d7

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

example/unicorn/components/objects.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from decimal import Decimal as D
23
from typing import Optional
34

45
from django.utils.timezone import now
@@ -32,11 +33,16 @@ class ObjectsView(UnicornView):
3233
dictionary = {"name": "dictionary", "nested": {"name": "nested dictionary"}}
3334
book = Book(title="The Sandman")
3435
books = Book.objects.all()
35-
3636
date_example = now()
37+
float_example: float = 1.1
38+
decimal_example = D("1.1")
39+
int_example = 4
3740

3841
def get_date(self):
3942
self.date_example = now()
4043

4144
def set_dictionary(self, val):
4245
self.dictionary = val
46+
47+
def add_one_to_float(self):
48+
self.float_example += 1

example/unicorn/templates/unicorn/objects.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@
8383
</ul>
8484
</div>
8585

86+
<div>
87+
<input u:model="float_example" type="text" id="floatExampleId">
88+
<br />
89+
<input u:model="decimal_example" type="text" id="decimalExampleId">
90+
<br />
91+
<input u:model="int_example" type="text" id="intExampleId">
92+
<br />
93+
94+
<button type="submit" u:click="add_one_to_float">Add 1 to float</button>
95+
<br />
96+
float_example: {{ float_example }}
97+
<br />
98+
</div>
99+
86100
<button u:click="$reset">$reset</button>
87101
<button u:click="$refresh">$refresh</button>
88102
</div>

0 commit comments

Comments
 (0)