Replies: 2 comments
-
|
I am getting closer to a solution... Here is what I did: # Install sample data
$ mkdir sample_data
$ wget -P sample_data/ https://raw.githubusercontent.com/saulpw/visidata/refs/heads/develop/sample_data/benchmark.csv
$ wget -P sample_data/ https://raw.githubusercontent.com/saulpw/visidata/refs/heads/develop/sample_data/sample.tsv
# Create config file
$ mkdir -p config/visidata
$ touch config/visidata/config.pyNow, in @Sheet.api
def editCell(self, vcolidx=None, rowidx=None, value=None, **kwargs):
'''Call vd.editText for the cell at (*rowidx*, *vcolidx*). Return the new value, properly typed.
- *vcolidx*: numeric index into ``self.availCols``. When None, use current column.
- *rowidx*: numeric index into ``self.rows``. If negative, indicates the column name in the header.
- *value*: if given, the starting input; otherwise the starting input is the cell value or column name as appropriate.
- *kwargs*: passthrough args to ``vd.editText``.
'''
reslist = ["apple", "banana", "cherry"]
res = reslist[random.randint(0,2)]
self.vd.status(f'gave you a random fruit')
return resI can now edit the sample data: If the user now presses What I would like to have: This is where I am stuck now. Does somebody have any ideas, e.g. how I can call the version of |
Beta Was this translation helpful? Give feedback.
-
|
Finally, I found some kind of solution. Below is my
@Sheet.api
def pickeditCell(self, vcolidx=None, rowidx=None, value=None, **kwargs):
'''Call vd.editText for the cell at (*rowidx*, *vcolidx*). Return the new value, properly typed.
- *vcolidx*: numeric index into ``self.availCols``. When None, use current column.
- *rowidx*: numeric index into ``self.rows``. If negative, indicates the column name in the header.
- *value*: if given, the starting input; otherwise the starting input is the cell value or column name as appropriate.
- *kwargs*: passthrough args to ``vd.editText``.
'''
# -------------------- lines taken from original _input.py -> editCell(...)
if vcolidx is None:
vcolidx = self.cursorVisibleColIndex
x, w = self._visibleColLayout.get(vcolidx, (0, 0))
col = self.availCols[vcolidx]
if rowidx is None:
rowidx = self.cursorRowIndex
if rowidx < 0: # header
vd.fail(f"Header can't be fuzzy-edited")
else:
y, h = self._rowLayout.get(rowidx, (0, 0))
value = value or col.getDisplayValue(self.rows[self.cursorRowIndex])
# update local bindings with kwargs.bindings instead of the inverse, to preserve kwargs.bindings for caller
# end----------------- lines taken from original _input.py -> editCell(...) ------------------------
# uniq_colvalues, value, col
uniq_colvalues = set(["apple", "banana", "cherry"])
choices = [AttrDict(key=v) for v in sorted(uniq_colvalues)]
preset = value if value in uniq_colvalues else ''
pick = self.inputPalette(
prompt=f'{col.name}: ',
items=choices,
value_key='key',
value=preset,
formatter=lambda m, item, trig: f'{trig} {item.key}' if item else ' ',
)
return pick # VisiData will write this value into the cell
@Sheet.api
def pickeditCellRepeat(sheet):
'Edit chosen column; after commit, move down and re-run until Esc.'
col = sheet.cursorCol
vcolidx = sheet.cursorVisibleColIndex
try:
val = sheet.pickeditCell(vcolidx)
except EscapeException:
return
# If user cancels, skip; otherwise set value and queue next iteration.
if val is not None:
col.setValues([sheet.cursorRow], val)
vd.queueCommand('go-down')
vd.queueCommand('pickedit-cell-repeat')
Sheet.addCommand('E', 'pickedit-cell-repeat', 'pickeditCellRepeat()', 'edit cell and, on Enter, move down and continue editing')However, after pressing |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to use visidata as a GUI for catagorizing my account data.
One problem I have is that I want to allow only certain values for column
category, e.g.foodorassurance.I could make this column numerical and input only numbers, e.g.
1forfoodetc. and join another table that holds semantic information.However, I wonder whether one can programmatically pose a restriction on column
categorysuch that when typingfooI can select from a listfoodmuch like the fuzzy search when one hitsspaceand tries to enter a command likecommand-log-allin command line.I found that
visidata/features/commandpalette.pyhas a functioninputPalettethat might already have implemented what I needed.visidata/_input.py:editCell(...)seems to be the function that is called upon a cell-edit. Is there a way to elegantly combine the functionality of those two, e.g. like placing s.th. likein
visidata/config.pyto overwrite the functionality ofeditCellin the case of columncategory?Can someone give me an idea how to tackle this problem or give me some hint? Maybe there is already a similar feature?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions