Skip to content

Commit b60b0bd

Browse files
isabelizimmjennybc
andauthored
data explorer: "Copy as Code" comms (#8536)
related issue: #8531 related pr for Ark: posit-dev/ark#873 I'm not exactly sure of the flow of these PRs with Ark, but I believe we'll want to merge that PR, release Ark, and then pick up the new release in this PR before this PR is merged. This PR implements makes 3 major changes to the comms: - **add `convert_to_code` as a supported feature:** there are other supported features, such as row filters and column filters. This structure includes if it is supported as well as supported syntaxes, as an array of values. - **`suggest_code_syntax` message:** for a particular data explorer instance, decide what options a user has for generating code. I would expect the return value of this to be `base-r` or something to that effect. - **`convert_to_code` message:** for a particular data explorer instance, this message sends over the filters and sorts applied to the data and the desired syntax. The expectation is that this will return a code snippet string. The front end will (eventually) apply code highlighting and formatting. Below is a diagram of the general flow through comms. Note that these comm messages will not call each other; they will be triggered by a modal in Positron core. ```mermaid flowchart TD A["Data explorer instance is created"] --> B["Get all potential syntaxes from language pack via the **supported_features**." ] B --> C["Also set suggested syntax with **suggest_code_syntax**"] C --> D[Open modal] D --> E["Use **dataExplorerInstance.suggestedSyntax**"] E --> F["Translate data view (filters, sorts) to code with **_translate_to_code_**"] F --> G[Display code for user to copy] G -->|user chooses another syntax from modal dropdown| F ``` There is an open PR that builds off this branch to use these comms, so you can actually fire them off/interact with them #8537 ### Release Notes <!-- Optionally, replace `N/A` with text to be included in the next release notes. The `N/A` bullets are ignored. If you refer to one or more Positron issues, these issues are used to collect information about the feature or bugfix, such as the relevant language pack as determined by Github labels of type `lang: `. The note will automatically be tagged with the language. These notes are typically filled by the Positron team. If you are an external contributor, you may ignore this section. --> #### New Features - N/A #### Bug Fixes - N/A ### QA Notes It doesn't look like we have much testing for comms, but open to ideas! Currently I plan on adding unit tests on each language runtime and then e2e tests for the UI pieces. --------- Signed-off-by: Isabel Zimmerman <54685329+isabelizimm@users.noreply.github.com> Co-authored-by: Jennifer (Jenny) Bryan <jenny.f.bryan@gmail.com>
1 parent daa9b1c commit b60b0bd

File tree

8 files changed

+503
-4
lines changed

8 files changed

+503
-4
lines changed

extensions/positron-python/python_files/posit/positron/data_explorer.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .data_explorer_comm import (
2828
ArraySelection,
2929
BackendState,
30+
CodeSyntaxName,
3031
ColumnDisplayType,
3132
ColumnFilter,
3233
ColumnFilterType,
@@ -45,6 +46,9 @@
4546
ColumnSortKey,
4647
ColumnSummaryStats,
4748
ColumnValue,
49+
ConvertedCode,
50+
ConvertToCodeFeatures,
51+
ConvertToCodeRequest,
4852
DataExplorerBackendMessageContent,
4953
DataExplorerFrontendEvent,
5054
DataSelectionCellRange,
@@ -81,6 +85,7 @@
8185
SetRowFiltersParams,
8286
SetSortColumnsFeatures,
8387
SetSortColumnsParams,
88+
SuggestCodeSyntaxRequest,
8489
SummaryStatsBoolean,
8590
SummaryStatsDate,
8691
SummaryStatsDatetime,
@@ -281,6 +286,12 @@ def get_schema(self, params: GetSchemaParams):
281286
def _get_single_column_schema(self, column_index: int) -> ColumnSchema:
282287
raise NotImplementedError
283288

289+
def suggest_code_syntax(self, request: SuggestCodeSyntaxRequest):
290+
raise NotImplementedError
291+
292+
def convert_to_code(self, request: ConvertToCodeRequest):
293+
raise NotImplementedError
294+
284295
def search_schema(self, params: SearchSchemaParams):
285296
filters = params.filters
286297
start_index = params.start_index
@@ -804,6 +815,13 @@ def _prof_histogram(
804815
support_status=SupportStatus.Unsupported,
805816
supported_formats=[],
806817
),
818+
convert_to_code=ConvertToCodeFeatures(
819+
support_status=SupportStatus.Supported,
820+
code_syntaxes=[
821+
CodeSyntaxName(code_syntax_name="pandas"),
822+
CodeSyntaxName(code_syntax_name="polars"),
823+
],
824+
),
807825
)
808826

809827

@@ -1350,6 +1368,16 @@ def schema_getter(column_name, column_index):
13501368

13511369
return schema_updated, new_state
13521370

1371+
def suggest_code_syntax(self, request: SuggestCodeSyntaxRequest): # noqa: ARG002
1372+
"""Returns the supported code types for exporting data."""
1373+
return CodeSyntaxName(code_syntax_name="pandas").dict()
1374+
1375+
def convert_to_code(self, request: ConvertToCodeRequest): # noqa: ARG002
1376+
"""Translates the current data view, including filters and sorts, into a code snippet."""
1377+
return ConvertedCode(
1378+
converted_code=["import pandas as pd", "# TODO: Implement export to code"]
1379+
).dict()
1380+
13531381
@classmethod
13541382
def _construct_schema(
13551383
cls, column, column_name, column_index: int, state: DataExplorerState
@@ -1911,6 +1939,10 @@ def _prof_histogram(
19111939
ExportFormat.Html,
19121940
],
19131941
),
1942+
convert_to_code=ConvertToCodeFeatures(
1943+
support_status=SupportStatus.Supported,
1944+
code_syntaxes=[CodeSyntaxName(code_syntax_name="pandas")],
1945+
),
19141946
)
19151947

19161948

@@ -2280,6 +2312,16 @@ def schema_getter(column_name, column_index):
22802312

22812313
return schema_updated, new_state
22822314

2315+
def suggest_code_syntax(self, request: SuggestCodeSyntaxRequest): # noqa: ARG002
2316+
"""Returns the supported code types for exporting data."""
2317+
return CodeSyntaxName(code_syntax_name="polars").dict()
2318+
2319+
def convert_to_code(self, request: ConvertToCodeRequest): # noqa: ARG002
2320+
"""Translates the current data view, including filters and sorts, into a code snippet."""
2321+
return ConvertedCode(
2322+
converted_code=["import polars as pl", "# TODO: Implement export to code"]
2323+
).dict()
2324+
22832325
def _get_single_column_schema(self, column_index: int):
22842326
if self.state.schema_cache:
22852327
return self.state.schema_cache[column_index]
@@ -2762,6 +2804,10 @@ def _prof_histogram(
27622804
supported_formats=[ExportFormat.Csv, ExportFormat.Tsv],
27632805
),
27642806
set_sort_columns=SetSortColumnsFeatures(support_status=SupportStatus.Supported),
2807+
convert_to_code=ConvertToCodeFeatures(
2808+
support_status=SupportStatus.Supported,
2809+
code_syntaxes=[CodeSyntaxName(code_syntax_name="polars")],
2810+
),
27652811
)
27662812

27672813

extensions/positron-python/python_files/posit/positron/data_explorer_comm.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,26 @@ class ExportedData(BaseModel):
252252
)
253253

254254

255+
class ConvertedCode(BaseModel):
256+
"""
257+
Code snippet for the data view
258+
"""
259+
260+
converted_code: List[StrictStr] = Field(
261+
description="Lines of code that implement filters and sort keys",
262+
)
263+
264+
265+
class CodeSyntaxName(BaseModel):
266+
"""
267+
Syntax to use for code conversion
268+
"""
269+
270+
code_syntax_name: StrictStr = Field(
271+
description="The name of the code syntax, eg, pandas, polars, dplyr, etc.",
272+
)
273+
274+
255275
class FilterResult(BaseModel):
256276
"""
257277
The result of applying filters to a table
@@ -972,6 +992,10 @@ class SupportedFeatures(BaseModel):
972992
description="Support for 'export_data_selection' RPC and its features",
973993
)
974994

995+
convert_to_code: ConvertToCodeFeatures = Field(
996+
description="Support for 'convert_to_code' RPC and its features",
997+
)
998+
975999

9761000
class SearchSchemaFeatures(BaseModel):
9771001
"""
@@ -1057,6 +1081,21 @@ class SetSortColumnsFeatures(BaseModel):
10571081
)
10581082

10591083

1084+
class ConvertToCodeFeatures(BaseModel):
1085+
"""
1086+
Feature flags for convert to code RPC
1087+
"""
1088+
1089+
support_status: SupportStatus = Field(
1090+
description="The support status for this RPC method",
1091+
)
1092+
1093+
code_syntaxes: Optional[List[CodeSyntaxName]] = Field(
1094+
default=None,
1095+
description="The syntaxes for converted code",
1096+
)
1097+
1098+
10601099
class TableSelection(BaseModel):
10611100
"""
10621101
A selection on the data grid, for copying to the clipboard or other
@@ -1208,6 +1247,12 @@ class DataExplorerBackendRequest(str, enum.Enum):
12081247
# Export data selection as a string in different formats
12091248
ExportDataSelection = "export_data_selection"
12101249

1250+
# Converts the current data view into a code snippet.
1251+
ConvertToCode = "convert_to_code"
1252+
1253+
# Suggest code syntax for code conversion
1254+
SuggestCodeSyntax = "suggest_code_syntax"
1255+
12111256
# Set column filters to select subset of table columns
12121257
SetColumnFilters = "set_column_filters"
12131258

@@ -1422,6 +1467,65 @@ class ExportDataSelectionRequest(BaseModel):
14221467
)
14231468

14241469

1470+
class ConvertToCodeParams(BaseModel):
1471+
"""
1472+
Converts filters and sort keys as code in different syntaxes like
1473+
pandas, polars, data.table, dplyr
1474+
"""
1475+
1476+
column_filters: List[ColumnFilter] = Field(
1477+
description="Zero or more column filters to apply",
1478+
)
1479+
1480+
row_filters: List[RowFilter] = Field(
1481+
description="Zero or more row filters to apply",
1482+
)
1483+
1484+
sort_keys: List[ColumnSortKey] = Field(
1485+
description="Zero or more sort keys to apply",
1486+
)
1487+
1488+
code_syntax_name: CodeSyntaxName = Field(
1489+
description="The code syntax to use for conversion",
1490+
)
1491+
1492+
1493+
class ConvertToCodeRequest(BaseModel):
1494+
"""
1495+
Converts filters and sort keys as code in different syntaxes like
1496+
pandas, polars, data.table, dplyr
1497+
"""
1498+
1499+
params: ConvertToCodeParams = Field(
1500+
description="Parameters to the ConvertToCode method",
1501+
)
1502+
1503+
method: Literal[DataExplorerBackendRequest.ConvertToCode] = Field(
1504+
description="The JSON-RPC method name (convert_to_code)",
1505+
)
1506+
1507+
jsonrpc: str = Field(
1508+
default="2.0",
1509+
description="The JSON-RPC version specifier",
1510+
)
1511+
1512+
1513+
class SuggestCodeSyntaxRequest(BaseModel):
1514+
"""
1515+
Suggest code syntax for code conversion based on the current backend
1516+
state
1517+
"""
1518+
1519+
method: Literal[DataExplorerBackendRequest.SuggestCodeSyntax] = Field(
1520+
description="The JSON-RPC method name (suggest_code_syntax)",
1521+
)
1522+
1523+
jsonrpc: str = Field(
1524+
default="2.0",
1525+
description="The JSON-RPC version specifier",
1526+
)
1527+
1528+
14251529
class SetColumnFiltersParams(BaseModel):
14261530
"""
14271531
Set or clear column filters on table, replacing any previous filters
@@ -1575,6 +1679,8 @@ class DataExplorerBackendMessageContent(BaseModel):
15751679
GetDataValuesRequest,
15761680
GetRowLabelsRequest,
15771681
ExportDataSelectionRequest,
1682+
ConvertToCodeRequest,
1683+
SuggestCodeSyntaxRequest,
15781684
SetColumnFiltersRequest,
15791685
SetRowFiltersRequest,
15801686
SetSortColumnsRequest,
@@ -1623,6 +1729,10 @@ class ReturnColumnProfilesParams(BaseModel):
16231729

16241730
ExportedData.update_forward_refs()
16251731

1732+
ConvertedCode.update_forward_refs()
1733+
1734+
CodeSyntaxName.update_forward_refs()
1735+
16261736
FilterResult.update_forward_refs()
16271737

16281738
BackendState.update_forward_refs()
@@ -1705,6 +1815,8 @@ class ReturnColumnProfilesParams(BaseModel):
17051815

17061816
SetSortColumnsFeatures.update_forward_refs()
17071817

1818+
ConvertToCodeFeatures.update_forward_refs()
1819+
17081820
TableSelection.update_forward_refs()
17091821

17101822
DataSelectionSingleCell.update_forward_refs()
@@ -1741,6 +1853,12 @@ class ReturnColumnProfilesParams(BaseModel):
17411853

17421854
ExportDataSelectionRequest.update_forward_refs()
17431855

1856+
ConvertToCodeParams.update_forward_refs()
1857+
1858+
ConvertToCodeRequest.update_forward_refs()
1859+
1860+
SuggestCodeSyntaxRequest.update_forward_refs()
1861+
17441862
SetColumnFiltersParams.update_forward_refs()
17451863

17461864
SetColumnFiltersRequest.update_forward_refs()

0 commit comments

Comments
 (0)