-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d0108f
commit d43f69a
Showing
11 changed files
with
164 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mesop as me | ||
|
||
|
||
def on_load(e: me.LoadEvent): | ||
pass | ||
|
||
|
||
# me.query_params["foo"] = "bar" | ||
# me.query_params["bar"] = "baz" | ||
|
||
|
||
@me.page(path="/examples/query_params", on_load=on_load) | ||
def page(): | ||
me.text(f"query params: {me.query_params}") | ||
me.button("increment_query_param by navigate", on_click=navigate) | ||
|
||
|
||
def navigate(e: me.ClickEvent): | ||
print('me.query_params["counter"]', me.query_params["counter"]) | ||
counter = int(me.query_params["counter"]) + 1 | ||
me.navigate("/examples/query_params?counter=" + str(counter)) |
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,30 @@ | ||
from typing import Iterator, MutableMapping | ||
|
||
from mesop.runtime import runtime | ||
|
||
|
||
class QueryParamsProxy(MutableMapping[str, str]): | ||
def __iter__(self) -> Iterator[str]: | ||
return iter(runtime().context().query_params()) | ||
|
||
def __len__(self) -> int: | ||
return len(runtime().context().query_params()) | ||
|
||
def __str__(self) -> str: | ||
return str(runtime().context().query_params()) | ||
|
||
def __getitem__(self, key: str) -> str: | ||
print( | ||
"runtime().context().query_params()", runtime().context().query_params() | ||
) | ||
# check if it should be the first or last item | ||
return runtime().context().query_params()[key][0] | ||
|
||
def __delitem__(self, key: str) -> None: | ||
runtime().context().set_query_param(key=key, value=None) | ||
|
||
def __setitem__(self, key: str, value: str) -> None: | ||
runtime().context().set_query_param(key=key, value=value) | ||
|
||
|
||
query_params = QueryParamsProxy() |
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
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
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,22 @@ | ||
import {QueryParam} from 'mesop/mesop/protos/ui_jspb_proto_pb/mesop/protos/ui_pb'; | ||
|
||
export function getQueryParams(): QueryParam[] { | ||
const queryString = window.location.search; | ||
const urlParams = new URLSearchParams(queryString); | ||
const params: Record<string, string[]> = {}; | ||
urlParams.forEach((value, key) => { | ||
if (!params[key]) { | ||
params[key] = [value]; | ||
} else { | ||
params[key].push(value); | ||
} | ||
}); | ||
const queryParams: QueryParam[] = []; | ||
for (const [key, values] of Object.entries(params)) { | ||
const queryParam = new QueryParam(); | ||
queryParam.setKey(key); | ||
queryParam.setValuesList(values); | ||
queryParams.push(queryParam); | ||
} | ||
return queryParams; | ||
} |