Jupyter/IPython custom autocomplete for cell magic - autocomplete

I have read several discussions about this, but I am a bit stuck with some more advanced behavior I am seeking. As a preamble, I am using
def load_ipython_extension(ipython):
def custom_complete(self, event):
return [list of completions]
ipython.set_hook('complete_command', table_complete, re_key = '%%magic')
load_ipython_extension(get_ipython())
to show autocomplete for a custom cell magic, but this only works on the same line (e.g. %%magic word), but not in the body of the cell...so how do I make that regex multiline to keep completing?

Related

TextKit 2 - How to create word level NSTextElements in order to tag words in text

I'm trying to create tags/labels/badges to tag certain words(actually sometimes multiple words can constitute a tag). Currently, I've added a custom NSAttributedString key that I have used to tag words with their part of speech. I want to tag these words with a rounded background coloring. I've gone through the code in this sample code, that creates tags like this:
But the example code just takes existing NSTextElements(paragraph level) and applies styling to them. When I plug into the same delegate functions from the text code all the text elements are paragraph level. For example
This is one.
This is two.
Would create two text elements: This is one. and This is two.. I'm curious how I can split these into multiple text elements. For example if I wanted to just tag EGG in EGG SANDWICH NO. 2, I'm guessing I need to split that text element into two(EGG and SANDWICH NO. 2), but I can't figure out how to do that currently, or find any examples of adding word tags to text on MacOS(The app does not need to support iOS, only MacOS).
Am I along the right track for accomplishing this? If so, what's the best way to split up the NSTextElements?
I had a similar problem and failed in a way that I really discourage to do that.
TextKit 2 is evolving. I report on trying to create a NSTextElement subclass below the NSTextParagraph level in macOS Ventana (autumn 2022).
My problem was to communicate the layout position of a text attachment, which is part of the attributed string, to the text view to position the associated subview and its exclusion path accordingly - a standard problem for text with figures.
The path to introduce a subclass of an NSTextElement into the text layout process is via delegates.
Starting at the text store:
NSTextContentStoreDelegate offers methods to introduce custom NSTextElements depending on the content of the NSAttributedString:
1. Paragraph Level:
func textContentStorage(NSTextContentStorage, textParagraphWith: NSRange) -> NSTextParagraph?
2. Arbitrary NSTextElement Level:
func textContentManager(NSTextContentManager, textElementAt: NSTextLocation) -> NSTextElement?
NSTextLayoutManagerDelegate can deliver custom NSTextLayoutFragment objects for these custom NSTextElements:
func textLayoutManager( _ textLayoutManager: NSTextLayoutManager, textLayoutFragmentFor location: NSTextLocation, in textElement: NSTextElement) -> NSTextLayoutFragment
The custom NSTextLayoutFragment objects can do what you desire.
I tried to subclass on the NSTextElement level (using method 2 of NSTextContentStoreDelegate) and used a custom NSTextLayoutFragment to execute my code. The result was that the entire layout process stopped when this custom NSTextLayoutFragment object was encountered. I overrode all methods that are in the documentation of the NSTextLayoutFragment to catch the problem and failed. Even worse, I introduced memory problems into my code that Swift as a language should guard against. At places where I and the compiler expected a model object the executing code saw the view which displays this model object.
I finally accepted that I can not go below the paragraph level and subclassed NSTextParagraph and introduced this class using the first and not the second NSTextContentStorageDelegate method. I solved the desired atomisation down to the attachment within the subclasses. That worked immediately and brought the desired functionality in a very precise and efficient way.

TreeView multiple selection does not work correctly after changing selection without UI

This is potentially a bug, though perhaps I'm misunderstanding something.
Brief description
Basically, I have found that using "Shift+Arrows" to do multiple selection in a Gtk.TreeView does not work correctly after changing the selection using Gtk.TreeSelection.select_iter. On the other hand, if you change the selection by clicking on a row and then pressing "Shift+Arrows", the selection behaves as one would expect.
I should note that if you change the selected row by calling Gtk.TreeSelection.select_iter, the UI updates as you would expect and calling Gtk.TreeSelection.get_selected_rows() returns the rows it should. It's only when you then try to select multiple rows using the arrow keys that you get strange behavior.
This is perhaps best illustrated in this self contained example, which I've tried to make as simple as possible:
Code
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class TreeViewBug(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.connect('destroy', Gtk.main_quit)
# Create model consisting of row path and a name
self.treeModel = Gtk.ListStore(int, str)
self.treeModel.append([0, 'alice'])
self.treeModel.append([1, 'bob'])
self.treeModel.append([2, 'chad'])
self.treeModel.append([3, 'dan'])
self.treeModel.append([4, 'emma'])
self.treeView = Gtk.TreeView()
self.treeView.append_column(Gtk.TreeViewColumn('path', Gtk.CellRendererText(), text=0))
self.treeView.append_column(Gtk.TreeViewColumn('name', Gtk.CellRendererText(), text=1))
self.treeView.set_model(self.treeModel)
# Allow for multiple selection
self.treeView.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
self.add(self.treeView)
def run(self):
self.show_all()
# Focus the TreeView so we can test multiple select via keyboard without clicking on a row
self.treeView.grab_focus()
# Manually change the selected row to the row with "chad"
chadIter = self.treeModel[2].iter
self.treeView.get_selection().select_iter(chadIter)
print('Press "Shift+Down" and see what happens')
print(' it should select "chad, dan", but instead it selects "bob, chad"')
print('Afterwards, try clicking on "chad" and then pressing Shift+Down. It should behave normally')
Gtk.main()
if __name__ == '__main__':
tv = TreeViewBug()
tv.run()
Things I've tried
I initially encountered the bug when my code changed the selected row via Gtk.TreeSelection.select_iter in response to a button click.
I've also tried:
adding a custom select function (Gtk.TreeSelection.set_select_function)
clearing the selection before changing it (Gtk.TreeSelection.unselect_all)
changing the selection asynchronously (GLib.idle_add).
redrawing TreeView after changing selection
Speculations
I'm guessing that TreeView/TreeViewSelection has some internal state variable tracking selection and row that, for some reason, isn't getting properly updated when TreeSelection.select_iter is called. These variables are probably related to UI features, because TreeSelection.get_selected_rows still works properly. Also it makes sense the UI would need additional state information since the UI logic of multiple selection depends on previous UI interaction (Shift+Down behaves differently when extending a selection depending on whether you initially selected upwards or downwards)
Because a Gtk.TreeView uses MVC, you actually need to set the cursor of the treeview. This may affect the rest of the program, depending on what you are doing. Example:
#chadIter = self.treeModel[2].iter
#self.treeView.get_selection().select_iter(chadIter)
path = 2
column = self.treeView.get_column(0)
edit = False
self.treeView.set_cursor(path, column, edit)

GWT Read-only TextArea will not resize

GWT newbie here. I have found that when I make a TextArea read-only useful features such as dynamic expansion and even word-wrapping stops working. My intention was to move a string from one text area, append it some way to some more strings, and then add this string into my read-only TextArea. I have doing something like this:
// Temporarily enable the field to set the value of the TextArea
logTextArea.setEnabled(true);
String remarks = // my string to add into the box
if (remarks.length() > 0) {
logTextArea.setEnabled.setValue(remarks);
}
// set read-only again
logTextArea.setEnabled.setEnabled(false);
I also have to work out how many lines I now span and explicitly set the height of the box (via setVisibleLines()). I have now found that it does not word-wrap, so I've had to add some more horrible bodge-code to further split up this string.
So I'm writing code to emulate functionality that comes for free on a normal writable TextArea. Has anyone else found this issue after setting a text-field read-only? Is there another widget I could possibly use to display a list of read-only strings that will auto resize and auto wrap for me?
Many thanks for your time,
tom
Text Area is fine for re-sizing and auto word wrap, even you have your text
area as read only.
Tested now by creating a test project for gwt and it is working fine.
Also Word Wrap is the default behavior of Text area if you want to turn it off then you need to explicitly do this "getElement().setAttribute("wrap","off");

Using Jasper ContitionalStyles with named style references

I've got a situation where I create a report based on jrxml-files generated programmaitically and a style template with a given name and given style names. This works fine up to the point where there are conditional styles. Every example I found just uses ConditionalStyles to change some value like setting a different backcolor for specific situations. This can't be done here since there is no way of knowing what separates an even row style from an odd row style. Like wheter an odd row will get a gray background or an even row will get a green foreground and a black bottom pen.
What I tried or looked at:
Using Conditional Styles - There seems to be no way of defining a style reference just to change style values
Using a Variable for the style name - The variable didn't get evaluated
Creating a style with 2 ConditionalStyles using even and odd styles as parent styles - these were ignored when creating the jrtx-file
So... is there a way to create alternate styled rows using style references? If yes how it is done?
As there seems no solution to this, we had to change the structure of the style-files to include the conditional style instead of doing this in the document.
When walking this path and trying to change or create these style-files programmatically, be aware that in the JRXmlTemplateWriter that comes with Jasper, the method toWriteConditionalStyles is implemented as:
protected boolean toWriteConditionalStyles() {
return false;
}
Which is unfortunate. You'll have to use your own Exporter that changes this behavior.
Simply the way of knowing what separates an even row style from an odd row style.
Even row will satisfy the condition
(($V{REPORT_COUNT}%2) == 0)
Odd row will satisfy the condition
(($V{REPORT_COUNT}%2) != 0)
Then you can use a Conditional Styles with above conditions

Perl HList: Change -background for individual items

I'm trying to alert the user that some data has been changed and needs to be saved. The data is displayed in Perl's Tk::HList box. I was hoping I could do:
if ($new_item) {
$HList->add($stock_no,-background=>"red");
}
or even:
if ($new_item) {
$HList->itemCreate($stock_no,0,-text=>$stock_no,-background=>"red");
}
but both throw
Tk::Error: Bad option `-background'
I've seen the idea to use ItemStyle but there's no clear answer if that works or not or if it's the best (and only) solution. Is there another way to highlight certain rows to alert the user?
It looks like it is the best way to change the background:
use Tk::ItemStyle;
my $alert = $mw->ItemStyle('text',-background=>"red");
$HList->itemCreate($stock_no,0,-style=>$alert);
I have to include that style to each item I add, there doesn't seem to be a way to do the whole row at once.