-
Notifications
You must be signed in to change notification settings - Fork 16
feat: Add ui.resolve to support URIs in dh.ui #1215
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
Changes from 14 commits
57d4ca7
daa7f03
b8b6931
cebf312
6db5dd2
ffffec6
a91fd15
fce3136
cab41be
9b5fdb9
2572afa
7b73bda
9f816fa
67c90fe
5499230
26be31b
6efa116
3359aff
cb01562
86538ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # URI | ||
|
|
||
| URIs are a way to reference Deephaven resources, such as tables or figures, from another instance. Deephaven UI has its own `ui.resolve` method that does not require server-to-server communication. Instead, the web client communicates directly with the appropriate server to get the resource. | ||
|
|
||
| ## Usage | ||
|
|
||
| Deephaven UI provides a `resolve` method (not to be confused with the `resolve` method from the [Deephaven URI package](/core/pydoc/code/deephaven.uri.html)) that allows you to reference Deephaven resources from other instances. Unlike the Deephaven URI package, `ui.resolve` does not resolve the URI to its resource on the server, so you cannot apply operations to the resource. | ||
|
|
||
| > [!NOTE] | ||
| > Currently, the only valid URIs for Deephaven UI are for Deephaven Enterprise Persistent Queries. | ||
| > See the [Deephaven Enterprise documentation](/enterprise/docs/deephaven-database/remote-tables-python/#uris) for more information on Persistent Query URIs. The optional parameters are ignored by `ui.resolve`. | ||
|
|
||
| ### Plain references | ||
|
|
||
| One way to use `ui.resolve` is to assign the reference to a variable, which the web UI will open just as if you created the resource. This can be useful if you want to display tables from multiple sources in a single dashboard without the worker defining the dashboard pulling the data from each source. | ||
|
|
||
| ```py order=null | ||
| from deephaven import ui | ||
|
|
||
| t = ui.resolve("pq://MyPersistentQuery/scope/table") # Can't do t.update() or any other operations | ||
| p = ui.resolve("pq://MyPersistentQuery/scope/plot") | ||
|
|
||
|
|
||
| @ui.component | ||
| def basic_dashboard(): | ||
| return ui.panel(ui.flex(t, p), title="Table and Plot") | ||
|
|
||
|
|
||
| my_dashboard = ui.dashboard(basic_dashboard()) | ||
| ``` | ||
|
|
||
| ### Usage in UI components | ||
|
|
||
| Some Deephaven UI components that accept tables as sources can also accept URIs. This includes [`ui.table`](table.md) and any components that accept an `item_table_source`. When using a URI with UI components, you can often just use the string without needing to call `ui.resolve`. However, if a component may take a string as a valid child (e.g., `ui.picker`), then you must use `ui.resolve` to distinguish between a string and a URI. You can always use `ui.resolve` in places where you can use just the string if you prefer to be explicit. | ||
|
|
||
| > [!WARNING] | ||
| > Deephaven UI URIs cannot be used as table sources in the Deephaven Express plotting library. | ||
|
|
||
| ```py order=null | ||
| from deephaven import ui | ||
|
|
||
| # You can use any ui.table props with a URI source | ||
| t = ui.table( | ||
| "pq://MyPersistentQuery/scope/table", | ||
| format_=ui.TableFormat(cols="A", background_color="salmon") | ||
| ) | ||
|
|
||
| # Must use ui.resolve because string is a valid child | ||
| picker = ui.picker( | ||
| ui.resolve("pq://MyPersistentQuery/scope/picker_table"), | ||
| label="Picker Table" | ||
| ) | ||
|
|
||
| list_view = ui.list_view( | ||
| ui.item_table_source( | ||
| "pq://MyPersistentQuery/scope/list_view_table", | ||
| key_column="Keys", | ||
| label_column="Labels" | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## URI Encoding | ||
|
|
||
| If your URI contains any special characters, such as spaces or slashes, you must encode the URI components using standard URL encoding. This is because URIs are often used in web contexts where special characters can cause issues. You can use Python's built-in `urllib.parse.quote` function to encode your URIs. | ||
|
|
||
| ```py order=null | ||
| from urllib.parse import quote | ||
| from deephaven import ui | ||
|
|
||
| # Encode the URI | ||
| pq_name = quote("My PQ/with spaces!", safe="") # safe="" will encode the forward slash | ||
mattrunyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| t = ui.resolve(f"pq://{pq_name}/scope/table") | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"file":"components/uri.md","objects":{"t":{"type":"deephaven.ui.Element","data":{"document":{"__dhElemName":"deephaven.ui.elements.UITable","props":{"table":{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/table"}},"format_":{"cols":"A","background_color":"salmon"},"showQuickFilters":false,"showGroupingColumn":true,"showSearch":false,"reverse":false}},"state":"{}"}},"picker":{"type":"deephaven.ui.Element","data":{"document":{"__dhElemName":"deephaven.ui.components.Picker","props":{"align":"start","direction":"bottom","shouldFlip":true,"label":"Picker Table","labelPosition":"top","children":{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/picker_table"}}}},"state":"{}"}},"list_view":{"type":"deephaven.ui.Element","data":{"document":{"__dhElemName":"deephaven.ui.components.ListView","props":{"density":"COMPACT","overflowMode":"truncate","selectionMode":"MULTIPLE","labelColumn":"Labels","keyColumn":"Keys","children":{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/list_view_table"}}}},"state":"{}"}}}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"file":"components/uri.md","objects":{"t":{"type":"deephaven.ui.Element","data":{"document":{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/table"}},"state":"{}"}},"p":{"type":"deephaven.ui.Element","data":{"document":{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/plot"}},"state":"{}"}},"my_dashboard":{"type":"deephaven.ui.Dashboard","data":{"document":{"__dhElemName":"deephaven.ui.components.Dashboard","props":{"children":{"__dhElemName":"__main__.basic_dashboard","props":{"children":{"__dhElemName":"deephaven.ui.components.Panel","props":{"title":"Table and Plot","direction":"column","alignItems":"start","gap":"size-100","overflow":"auto","padding":"size-100","children":{"__dhElemName":"deephaven.ui.components.Flex","props":{"gap":"size-100","flex":"auto","children":[{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/table"}},{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://MyPersistentQuery/scope/plot"}}]}}}}}}}},"state":"{}"}}}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"file":"components/uri.md","objects":{"t":{"type":"deephaven.ui.Element","data":{"document":{"__dhElemName":"deephaven.ui.elements.UriElement","props":{"uri":"pq://My%20PQ%2Fwith%20spaces%21/scope/table"}},"state":"{}"}}}} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from .Element import Element, PropsType | ||
| from .._internal import RenderContext | ||
|
|
||
|
|
||
| class UriElement(Element): | ||
| """ | ||
| Represents a remote object to be fetched by the client. | ||
|
|
||
| Args: | ||
| uri: The URI to fetch. | ||
| key: An optional key for the element. | ||
| """ | ||
|
|
||
| _uri: str | ||
|
|
||
| _key: str | None = None | ||
|
|
||
| def __init__(self, uri: str, key: str | None = None): | ||
| self._uri = uri | ||
| self._key = key | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return "deephaven.ui.elements.UriElement" | ||
|
|
||
| @property | ||
| def key(self) -> str | None: | ||
| return self._key | ||
|
|
||
| def render(self, context: RenderContext) -> PropsType: | ||
| return {"uri": self._uri} | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| if not isinstance(other, UriElement): | ||
| return False | ||
| return self._uri == other._uri and self._key == other._key | ||
|
|
||
|
|
||
| def resolve(uri: str) -> UriElement: | ||
| """ | ||
| Resolve a URI to a UriNode which can be used to fetch an object on the client from another query. | ||
|
|
||
| Args: | ||
| uri: The URI to resolve. | ||
|
|
||
| Returns: | ||
| A UriNode with the given URI. | ||
| """ | ||
| return UriElement(uri) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,14 +8,18 @@ import { | |
| ComboBoxProps as DHComboBoxJSApiProps, | ||
| } from '@deephaven/jsapi-components'; | ||
| import { isElementOfType } from '@deephaven/react-hooks'; | ||
| import type { dh } from '@deephaven/jsapi-types'; | ||
| import { ApiContext } from '@deephaven/jsapi-bootstrap'; | ||
| import { getSettings, RootState } from '@deephaven/redux'; | ||
| import { | ||
| SerializedPickerProps, | ||
| usePickerProps, | ||
| WrappedDHPickerJSApiProps, | ||
| } from './hooks/usePickerProps'; | ||
| import ObjectView from './ObjectView'; | ||
| import { useReExportedTable } from './hooks/useReExportedTable'; | ||
| import { useObjectViewObject } from './hooks/useObjectViewObject'; | ||
| import UriObjectView from './UriObjectView'; | ||
| import WidgetErrorView from '../widget/WidgetErrorView'; | ||
|
|
||
| export function ComboBox( | ||
| props: SerializedPickerProps< | ||
|
|
@@ -25,15 +29,33 @@ export function ComboBox( | |
| const settings = useSelector(getSettings<RootState>); | ||
| const { children, ...pickerProps } = usePickerProps(props); | ||
|
|
||
| const isObjectView = isElementOfType(children, ObjectView); | ||
| const table = useReExportedTable(children); | ||
| const isObjectView = | ||
| isElementOfType(children, ObjectView) || | ||
| isElementOfType(children, UriObjectView); | ||
| const { | ||
| widget: table, | ||
| api, | ||
| isLoading, | ||
| error, | ||
| } = useObjectViewObject<dh.Table>(children); | ||
|
|
||
| if (isObjectView) { | ||
| return ( | ||
| table && ( | ||
| if (error != null) { | ||
| return <WidgetErrorView error={error} />; | ||
| } | ||
| if (isLoading || table == null || api == null) { | ||
| return ( | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| <DHComboBox loadingState="loading" {...pickerProps}> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When does this state actually occur? And I was thinking we'd want something similar for the error state too, rather than the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is somewhat visible when loading from a PQ sometimes (especially first fetch) It's possibly more visible when loading via name because the check has been modified to ensure the serial doesn't exist in |
||
| {[]} | ||
| </DHComboBox> | ||
| ); | ||
| } | ||
| return ( | ||
| <ApiContext.Provider value={api}> | ||
| {/* eslint-disable-next-line react/jsx-props-no-spreading */} | ||
| <DHComboBoxJSApi {...pickerProps} table={table} settings={settings} /> | ||
| ) | ||
| </ApiContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
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.
Mixed feelings here... should we be explaining this on the Core side? Or on the Enterprise side? We really should have a utility method that accepts the query name and object name to make the URI for you, with proper encoding,
ui.query_resolveor something, e.g.ui.query_resolve(query="MyQueryName", widget="MyWidget"). Technically it'd be Core+ only, but I wouldn't feel terrible about putting it in Core anyways...As for this part - "If your URI contains any special characters" - moreso should be "You can construct a URI with any query name. If your query name contains special characters, such as spaces or slashes, you must encode the name first to avoid mangling the URL". (But nicer)