Skip to content

Commit

Permalink
Add an example of using type hint to convert string to float.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill committed Feb 18, 2021
1 parent 5bb6da5 commit 8fd47d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion example/unicorn/components/objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from decimal import Decimal as D
from typing import Optional

from django.utils.timezone import now
Expand Down Expand Up @@ -32,11 +33,16 @@ class ObjectsView(UnicornView):
dictionary = {"name": "dictionary", "nested": {"name": "nested dictionary"}}
book = Book(title="The Sandman")
books = Book.objects.all()

date_example = now()
float_example: float = 1.1
decimal_example = D("1.1")
int_example = 4

def get_date(self):
self.date_example = now()

def set_dictionary(self, val):
self.dictionary = val

def add_one_to_float(self):
self.float_example += 1
14 changes: 14 additions & 0 deletions example/unicorn/templates/unicorn/objects.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@
</ul>
</div>

<div>
<input u:model="float_example" type="text" id="floatExampleId">
<br />
<input u:model="decimal_example" type="text" id="decimalExampleId">
<br />
<input u:model="int_example" type="text" id="intExampleId">
<br />

<button type="submit" u:click="add_one_to_float">Add 1 to float</button>
<br />
float_example: {{ float_example }}
<br />
</div>

<button u:click="$reset">$reset</button>
<button u:click="$refresh">$refresh</button>
</div>

0 comments on commit 8fd47d7

Please sign in to comment.