You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/parameters.md
+139-3Lines changed: 139 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ view.py will ensure that the type sent to the server is compatible with what you
78
78
79
79
```py
80
80
@app.get("/")
81
-
@query("number", int)
81
+
@app.query("number", int)
82
82
asyncdefindex(number: int):
83
83
# number will always be an int.
84
84
# if it isn't, an error 400 is sent back to the user automatically
@@ -94,12 +94,16 @@ The following types are supported:
94
94
-`float`
95
95
-`dict` (or `typing.Dict`)
96
96
-`None`
97
+
- Pydantic Models
98
+
- Dataclasses
99
+
-`typing.TypedDict`
100
+
-`NamedTuple`
97
101
98
102
You can allow unions by just passing more parameters:
99
103
100
104
```py
101
105
@app.get('/hello')
102
-
@query("name", str, None)
106
+
@app.query("name", str, None)
103
107
asyncdefhello(name: str|None):
104
108
ifnot name:
105
109
return"hello world"
@@ -111,11 +115,143 @@ You can pass type arguments to a `dict`, which are also validated by the server:
111
115
112
116
```py
113
117
@app.get("/something")
114
-
@body("data", dict[str, int]) # typing.Dict on 3.8 and 3.9
118
+
@app.body("data", dict[str, int]) # typing.Dict on 3.8 and 3.9
115
119
asyncdefsomething(data: dict[str, int]):
116
120
# data will always be a dictionary of strings and integers
117
121
return"..."
118
122
```
119
123
120
124
The key in a dictionary must always be `str` (i.e. `dict[int, str]` is not allowed), but the value can be any supported type (including other dictionaries!)
121
125
126
+
### Objects
127
+
128
+
Here's an example of using an object type with `dataclasses`:
129
+
130
+
```py
131
+
from view import new_app, query
132
+
from dataclasses import dataclass, field
133
+
134
+
app = new_app()
135
+
136
+
defnow() -> str:
137
+
...# Calculate current time
138
+
139
+
@dataclass
140
+
classPost:
141
+
content: str
142
+
created_at = field(default_factory=now)
143
+
144
+
145
+
@app.post("/create")
146
+
@app.query("data", Post)
147
+
asyncdefcreate(data: Post):
148
+
print(f"Created post {data.created_at}")
149
+
return"Success", 201
150
+
```
151
+
152
+
You may also have recursive types, like so:
153
+
154
+
```py
155
+
classMyOtherObject(NamedTuple):
156
+
something: int
157
+
158
+
classMyObject(NamedTuple):
159
+
something: str
160
+
another_thing: MyOtherObject
161
+
```
162
+
163
+
### Typed Dictionaries
164
+
165
+
You may use `typing.TypedDict` to type your dictionary inputs if you don't want to use a basic `dict[..., ...]` (or `typing.Dict`), like so:
166
+
167
+
```py
168
+
from view import new_app
169
+
from typing import TypedDict
170
+
171
+
app = new_app()
172
+
173
+
classMyDict(TypedDict):
174
+
a: str
175
+
b: int
176
+
177
+
@app.get("/")
178
+
@app.query("data", MyDict)
179
+
asyncdefindex(data: MyDict):
180
+
return data["a"]
181
+
182
+
app.run()
183
+
```
184
+
185
+
You may also use `NotRequired` to allow certain keys to get omitted:
186
+
187
+
```py
188
+
classMyDict(TypedDict):
189
+
a: str
190
+
b: NotRequired[int]
191
+
```
192
+
193
+
## View Body Protocol
194
+
195
+
If you would like to create your own object that gets validated by view.py, you may use the `__view_body__` protocol.
196
+
197
+
A `__view_body__` should contain a dictionary containing the keys and their corresponding types, like so:
198
+
199
+
```py
200
+
from view import new_app
201
+
202
+
app = new_app()
203
+
204
+
classMyObject:
205
+
__view_body__ = {"a": str, "b": int}
206
+
207
+
@app.get("/")
208
+
@app.query("data", MyObject)
209
+
asyncdefindex(data: MyObject):
210
+
...
211
+
212
+
app.run()
213
+
```
214
+
215
+
The above would ensure the body contains something like the following in JSON:
216
+
217
+
```json
218
+
{
219
+
"data": {
220
+
"a": "...",
221
+
"b": 0
222
+
}
223
+
}
224
+
```
225
+
226
+
A default type can be annotated via `view.BodyParam`:
Copy file name to clipboardExpand all lines: docs/running.md
-7Lines changed: 0 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,5 @@
1
1
# Running
2
2
3
-
Is your view app running, you better go catch it!
4
-
5
-
!!! danger "Apologies"
6
-
7
-
I'm sorry, you may make a PR to delete this.
8
-
9
-
10
3
## Serving
11
4
12
5
You can simply run a view app via the `view serve` command, but if you're like me and would rather to just use the `python3` command, that's just as easy.
0 commit comments