-
-
Notifications
You must be signed in to change notification settings - Fork 7
Add docs #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add docs #17
Conversation
README.md
Outdated
@component | ||
def root(): | ||
return simple.router( | ||
route("/", home()), | ||
route( | ||
"/messages", | ||
all_messages(), | ||
# we'll improve upon these manually created routes in the next section... | ||
route("/with/Alice", messages_with("Alice")), | ||
route("/with/Alice-Bob", messages_with("Alice", "Bob")), | ||
), | ||
route("*", html.h1("Missing Link 🔗💥")), | ||
) | ||
|
||
@component | ||
def home(): | ||
return html.div( | ||
html.h1("Home Page 🏠"), | ||
link("Messages", to="/messages"), | ||
) | ||
|
||
@component | ||
def all_messages(): | ||
last_messages = { | ||
", ".join(msg["with"]): msg | ||
for msg in sorted(message_data, key=lambda m: m["id"]) | ||
} | ||
return html.div( | ||
html.h1("All Messages 💬"), | ||
html.ul( | ||
[ | ||
html.li( | ||
{"key": msg["id"]}, | ||
html.p( | ||
link( | ||
f"Conversation with: {', '.join(msg['with'])}", | ||
to=f"/messages/with/{'-'.join(msg['with'])}", | ||
), | ||
), | ||
f"{'' if msg['from'] is None else '🔴'} {msg['message']}", | ||
) | ||
for msg in last_messages.values() | ||
] | ||
), | ||
) | ||
|
||
@component | ||
def messages_with(*names): | ||
names = set(names) | ||
messages = [msg for msg in message_data if set(msg["with"]) == names] | ||
return html.div( | ||
html.h1(f"Messages with {', '.join(names)} 💬"), | ||
html.ul( | ||
[ | ||
html.li( | ||
{"key": msg["id"]}, | ||
f"{msg['from'] or 'You'}: {msg['message']}", | ||
) | ||
for msg in messages | ||
] | ||
), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think defining messages_with
, all_messages
, and message_data
is overkill. Ends up distracting from the point (nested routes).
Since this is simply for demonstration purposes, I would either rewrite messages_with
and all_messages
to return one-liner dummy strings, or not use components at all and directly put raw strings into the routes (ex. html.p("EXAMPLE: All Messages")
).
README.md
Outdated
``` | ||
|
||
In this case, `param` is the name of the route parameter and the optionally declared | ||
`type` specifies what kind of parameter it is. The available parameter types and what | ||
patterns they match are are: | ||
|
||
- str (default) - `[^/]+` | ||
- int - `\d+` | ||
- float - `\d+(\.\d+)?` | ||
- uuid - `[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}` | ||
- path - `.+` | ||
|
||
Any parameters that have matched in the currently displayed route can then be consumed | ||
with the `use_params` hook which returns a dictionary mapping the parameter names to | ||
their values. Note that parameters with a declared type will be converted to is in the | ||
parameters dictionary. So for example `/my/route/{my_param:float}` would match | ||
`/my/route/3.14` and have a parameter dictionary of `{"my_param": 3.14}`. | ||
|
||
If we take this information and apply it to our growing example application we'd | ||
substitute the manually constructed `/messages/with` routes with a single | ||
`/messages/with/{names}` route: | ||
|
||
```python | ||
from reactpy import component, html, run | ||
from reactpy_router import route, simple, link | ||
from reactpy_router.core import use_params | ||
|
||
message_data = [ | ||
{"id": 1, "with": ["Alice"], "from": None, "message": "Hello!"}, | ||
{"id": 2, "with": ["Alice"], "from": "Alice", "message": "How's it going?"}, | ||
{"id": 3, "with": ["Alice"], "from": None, "message": "Good, you?"}, | ||
{"id": 4, "with": ["Alice"], "from": "Alice", "message": "Good, thanks!"}, | ||
{"id": 5, "with": ["Alice", "Bob"], "from": None, "message": "We meeting now?"}, | ||
{"id": 6, "with": ["Alice", "Bob"], "from": "Alice", "message": "Not sure."}, | ||
{"id": 7, "with": ["Alice", "Bob"], "from": "Bob", "message": "I'm here!"}, | ||
{"id": 8, "with": ["Alice", "Bob"], "from": None, "message": "Great!"}, | ||
] | ||
|
||
@component | ||
def root(): | ||
return simple.router( | ||
route("/", home()), | ||
route( | ||
"/messages", | ||
all_messages(), | ||
route("/with/{names}", messages_with()), # note the path param | ||
), | ||
route("*", html.h1("Missing Link 🔗💥")), | ||
) | ||
|
||
@component | ||
def home(): | ||
return html.div( | ||
html.h1("Home Page 🏠"), | ||
link("Messages", to="/messages"), | ||
) | ||
|
||
@component | ||
def all_messages(): | ||
last_messages = { | ||
", ".join(msg["with"]): msg | ||
for msg in sorted(message_data, key=lambda m: m["id"]) | ||
} | ||
return html.div( | ||
html.h1("All Messages 💬"), | ||
html.ul( | ||
[ | ||
html.li( | ||
{"key": msg["id"]}, | ||
html.p( | ||
link( | ||
f"Conversation with: {', '.join(msg['with'])}", | ||
to=f"/messages/with/{'-'.join(msg['with'])}", | ||
), | ||
), | ||
f"{'' if msg['from'] is None else '🔴'} {msg['message']}", | ||
) | ||
for msg in last_messages.values() | ||
] | ||
), | ||
) | ||
|
||
@component | ||
def messages_with(): | ||
names = set(use_params()["names"].split("-")) # and here we use the path param | ||
messages = [msg for msg in message_data if set(msg["with"]) == names] | ||
return html.div( | ||
html.h1(f"Messages with {', '.join(names)} 💬"), | ||
html.ul( | ||
[ | ||
html.li( | ||
{"key": msg["id"]}, | ||
f"{msg['from'] or 'You'}: {msg['message']}", | ||
) | ||
for msg in messages | ||
] | ||
), | ||
) | ||
|
||
run(root) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is WAY too long to explain the simple concept of encoding parameters. Needs to be rewritten with the most basic example possible.
Just need to get the idea across that parameters turn into args
(maybe they can also become kwargs
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I think what I've written here is too long for a quick README that covers the features at a glance. However, this would still be useful to preserve, if explanations were expanded upon a bit, as a more in-depth tutorial. Is this something that you'd be comfortable with reworking, maybe with mkdocs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah when I get around to adding proper docs for this, I'll reference the commit log on this PR to fetch these examples. ReactPy docs and conreq dev comes first though.
Just switched the docs to mkdocs. Gonna merge as-is for now. |
closes: #1