Skip to content

Commit e023671

Browse files
authored
New Sort fields preparation block
1 parent 5e088ea commit e023671

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

Preparation/Sort-fields/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Sort fields in the input data by name or type.

Preparation/Sort-fields/manifest.json

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"@visokiotype": "CustomBlockSchema.CustomBlockManifest",
3+
"name": "Sort fields",
4+
"scriptFilename": "script.py",
5+
"language": "PYTHON",
6+
"executableVersion": null,
7+
"minVersions": [
8+
null
9+
],
10+
"optionsVersion": 1,
11+
"apiVersion": "VERSION_0",
12+
"isResourceIntensiveScript": false,
13+
"showPartitioning": false,
14+
"icon": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALUAAADiCAMAAAA/BnbUAAABQVBMVEUAAABYWFhfX18SEhLGxsYlJSXa2tpfX1+cnJxpaWm/v7+jo6MnJydbW1uysrJAQEC7u7uhoaGJiYmTk5NmZma9vb2+vr6jo6Ourq4wMDA2NjZKSkqzs7O6urqysrKZmZl1dXVNTU06OjqCgoJ4eHjR0dGDg4O5ubltbW1SUlJLS0tHR0ecnJzPz8+RkZHZ2dl7e3tPT0+IiIgqKiptbW0tLS3U1NTGxsaqqqqpqanBwcGXl5fg4OCHh4djY2NYWFg/Pz8mJiYwMDBiYmIpKSkRERGlpaVgYGDExMTd3d0+Pj7IyMjFxcVcXFxbW1uenp6RkZGNjY1ra2tUVFQhISEkJCQlJSV3d3cRERH////9/f3Nzc37+/v5+fn19fXr6+vn5+fS0tL4+Pjy8vLu7u7j4+Pg4ODIyMjX19fMzMza2tqgUoyAAAAAWXRSTlMAFeoK9Hz+JSXa/doTJhUT/dolFRT+/ezq2dgU/v3s6iYlJRsV/ezr6trY2P3x6+rq6tra2FX9/f397Ozq2trZ2NhAJSUG/f317e3q6urq2tra2ti/g3k6B2w6rCoAAAP2SURBVHja7Npnc9owG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Uhbbau/t3fHKADCUBQEvab3v4i1ogHBIhvn9cKUNtn/Tr1Rf/cdNTU1NTU19Q/UjVE/jpp6PGrq8agXUjf/Q6ipqampqampqampqampqampqampqampqampqampqampqampqampW+o1X2VSU58XfUW/RrFgQvV+XbbE0ayeRAszzZrP1OWkG1y6UhUtgjXra9HSXbMqmHmhtEJ3MsJOBlWT8dpkKDgZZU4GsIu18WTYvVjRTx4sCF6HCJ7i6N09GboOwfZZ9Zzl+4oAAAAASUVORK5CYII=",
15+
"description": "Sort fields in the input data by name or type",
16+
"category": "Preparation",
17+
"subcategory": null,
18+
"tags": [
19+
"sort",
20+
"fields",
21+
"sort fields"
22+
],
23+
"introductoryText": "### Sort fields in the input data by a criteria (alphabetical, type)",
24+
"dependencies": "",
25+
"options": [
26+
{
27+
"name": "sortBy",
28+
"title": "Sort Fields by",
29+
"description": "Sort fields by criteria",
30+
"groupTitle": null,
31+
"width": "ONE",
32+
"@visokiotype": "CustomBlockSchema.ChoiceCustomBlockPublicOption",
33+
"mandatory": true,
34+
"choices": [
35+
{
36+
"name": null,
37+
"title": "Name",
38+
"description": null,
39+
"groupTitle": null,
40+
"width": null,
41+
"@visokiotype": "CustomBlockSchema.ChoiceMetaCustomBlockPublicOption",
42+
"value": "name",
43+
"isSpecial": false
44+
},
45+
{
46+
"name": null,
47+
"title": "Type",
48+
"description": null,
49+
"groupTitle": null,
50+
"width": null,
51+
"@visokiotype": "CustomBlockSchema.ChoiceMetaCustomBlockPublicOption",
52+
"value": "type",
53+
"isSpecial": false
54+
}
55+
],
56+
"isDropdown": true,
57+
"choicesLayoutType": "LIST",
58+
"defaultValue": "name"
59+
}
60+
],
61+
"blockOutputs": [
62+
{
63+
"@visokiotype": "CustomBlockSchema.BlockOutputPublicOption",
64+
"id": "Output Data",
65+
"label": "1",
66+
"displayName": "Output 1",
67+
"tooltip": null
68+
}
69+
],
70+
"docker": {
71+
"@visokiotype": "CustomBlockSchema.DockerCustomBlockPublicOption",
72+
"customBaseImage": null,
73+
"useCustomBaseImage": false,
74+
"customSystemLibraries": null,
75+
"installVisokioRepLibraries": false
76+
},
77+
"designLock": false,
78+
"apiMode": "STREAM_TRANSFORM"
79+
}

Preparation/Sort-fields/script.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pandas as pd
2+
from omniscope.api import OmniscopeApi
3+
4+
# Initialize Omniscope API
5+
omniscope_api = OmniscopeApi()
6+
7+
# Function to sort dataframe columns based on given option
8+
def sort_dataframe_columns(df, sort_by='name'):
9+
if sort_by == 'name':
10+
sorted_columns = sorted(df.columns)
11+
elif sort_by == 'type':
12+
sorted_columns = sorted(df.dtypes.items(), key=lambda x: str(x[1]))
13+
sorted_columns = [col[0] for col in sorted_columns]
14+
else:
15+
raise ValueError("sort_by must be either 'name' or 'type'")
16+
17+
return df[sorted_columns]
18+
19+
# Fetch the sorting option ('name' or 'type') from the block options
20+
sort_by_option = omniscope_api.get_option('sortBy')
21+
22+
# Process the data stream using the lambda function
23+
omniscope_api.process_stream(lambda data: sort_dataframe_columns(data, sort_by=sort_by_option))
24+
25+
# Close the API after processing is complete
26+
omniscope_api.close()

0 commit comments

Comments
 (0)