configure_traits freezes console in Spyder/Anaconda - traitsui

Trying the very first example in traitsui doc:
from traits.api import HasTraits, Str, Int
from traitsui.api import View, Item
import traitsui
class SimpleEmployee(HasTraits):
first_name = Str
last_name = Str
department = Str
employee_number = Str
salary = Int
view1 = View(Item(name = 'first_name'),
Item(name = 'last_name'),
Item(name = 'department'))
sam = SimpleEmployee()
sam.configure_traits(view=view1)
makes the Spyder IPython console hang, even after the UI window has been closed.
MacOSX 10.14.6, Spyder 4.0.0, Python 3.7.0, IPython 7.10.2, traitsui 6.1.3
Probably something to configure about the UI event loop, but what and how ?

The difference between the Canopy IPython console and the Spyder IPython console is that
app = QtGui.QApplication.instance()
print(app)
returns
<PyQt5.QtWidgets.QApplication at 0xXXXXXXXX>
for both, but
is_event_loop_running_qt4(app)
returns Truein Canopy IPython terminal and False in Spyder IPython terminal.
It is necessary to patch view_application() in traits.qt4.view_application by commenting out the lines starting a new ViewApplication instance depending on that test. Instead of changing the original file, it is possible to run the following code:
# -*- coding: utf-8 -*-
"""Avoid Spyder console to hang when using configure_traits()
"""
from IPython import get_ipython
from pyface.qt import QtCore, QtGui, qt_api
from pyface.util.guisupport import is_event_loop_running_qt4
from traitsui.api import toolkit
from traitsui.qt4.view_application import ViewApplication
KEEP_ALIVE_UIS = set()
def on_ui_destroyed(object, name, old, destroyed):
""" Remove the UI object from KEEP_ALIVE_UIS.
"""
assert name == 'destroyed'
if destroyed:
assert object in KEEP_ALIVE_UIS
KEEP_ALIVE_UIS.remove(object)
object.on_trait_change(on_ui_destroyed, 'destroyed', remove=True)
def _view_application(context, view, kind, handler, id, scrollable, args):
""" Creates a stand-alone PyQt application to display a specified traits UI
View.
Parameters
----------
context : object or dictionary
A single object or a dictionary of string/object pairs, whose trait
attributes are to be edited. If not specified, the current object is
used.
view : view object
A View object that defines a user interface for editing trait attribute
values.
kind : string
The type of user interface window to create. See the
**traitsui.view.kind_trait** trait for values and
their meanings. If *kind* is unspecified or None, the **kind**
attribute of the View object is used.
handler : Handler object
A handler object used for event handling in the dialog box. If
None, the default handler for Traits UI is used.
scrollable : Boolean
Indicates whether the dialog box should be scrollable. When set to
True, scroll bars appear on the dialog box if it is not large enough
to display all of the items in the view at one time.
"""
if (kind == 'panel') or ((kind is None) and (view.kind == 'panel')):
kind = 'modal'
# !!!!!!! The following 4 lines commented out !!!!!!!!!!!
# app = QtGui.QApplication.instance()
# if app is None or not is_event_loop_running_qt4(app):
# return ViewApplication(context, view, kind, handler, id,
# scrollable, args).ui.result
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ui = view.ui(context,
kind=kind,
handler=handler,
id=id,
scrollable=scrollable,
args=args)
# If the UI has not been closed yet, we need to keep a reference to
# it until it does close.
if not ui.destroyed:
KEEP_ALIVE_UIS.add(ui)
ui.on_trait_change(on_ui_destroyed, 'destroyed')
return ui.result
def view_application(self, context, view, kind=None, handler=None,
id='', scrollable=None, args=None):
""" Creates a PyQt modal dialog user interface that
runs as a complete application, using information from the
specified View object.
Parameters
----------
context : object or dictionary
A single object or a dictionary of string/object pairs, whose trait
attributes are to be edited. If not specified, the current object is
used.
view : view or string
A View object that defines a user interface for editing trait
attribute values.
kind : string
The type of user interface window to create. See the
**traitsui.view.kind_trait** trait for values and
their meanings. If *kind* is unspecified or None, the **kind**
attribute of the View object is used.
handler : Handler object
A handler object used for event handling in the dialog box. If
None, the default handler for Traits UI is used.
id : string
A unique ID for persisting preferences about this user interface,
such as size and position. If not specified, no user preferences
are saved.
scrollable : Boolean
Indicates whether the dialog box should be scrollable. When set to
True, scroll bars appear on the dialog box if it is not large enough
to display all of the items in the view at one time.
"""
return _view_application(context, view, kind, handler,
id, scrollable, args)
from traitsui.qt4.toolkit import GUIToolkit
GUIToolkit.view_application = view_application
Thus, the behavior is OK in both Spyder and Canopy IPython consoles. Note that configure_traits(kind='modal') is also OK in both.

Related

Open Web Components Testing / Lit - component not being rendered?

I'm trying to test my Lit components with #open-wc/testing. Lit has an example repo here, with this test:
https://github.com/lit/lit-element-starter-ts/blob/main/src/test/my-element_test.ts#L44
When I try to render my element like they do in their example, I get this error:
jtests/components/coding-editor.test.ts:
🚧 Browser logs:
HTMLElement: <coding-editor></coding-editor>
❌ renders
TypeError: Cannot read properties of null (reading 'querySelector')
at o.<anonymous> (jtests/components/coding-editor.test.ts:16:30)
My component works in the browser and uses the name "coding-editor". It's as if this test renderer has no idea that I'm using a custom component though. I don't know why shadowRoot is null in my case.
My code is roughly this:
import { CodingEditor } from '../../app/javascript/components/coding-editor';
import {expect, fixture} from '#open-wc/testing';
import {html} from 'lit/static-html.js';
it('renders', async () => {
const el = await fixture(html`
<coding-editor></coding-editor>
`) as CodingEditor;
console.log(el);
const text = el.shadowRoot!.querySelector('.table-constrainer');
// expect(text).to.not.be.null
});
How can I get my test to render this properly, with the shadowRoot populated?
This is likely due to TypeScript removing the CodingEditor import that's only used as a type so the side effect of defining the custom element is not happening.
You can either set the TS compiler option importsNotUsedAsValues to preserve (See https://www.typescriptlang.org/tsconfig/#importsNotUsedAsValues) or add another import line for the side-effect like
import '../../app/javascript/components/coding-editor';
Additional explanation here too: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-imports-being-elided-in-my-emit
As a side-note, in the starter example you linked to, the imported class is used in assert.instanceOf as a value so it does not get elided by TypeScript.

Jupyter DataViewer does not offer view to show non basetype objects

I am using a jupyter notebook in Visual Studio Code.
In normal case when for example running a cell that contains a list or similar
my_test_list : list = [1,2,3]
I get the option Show variable in data viewer in the Window Juyper: Variables
But when I have an objects of one of my own classes:
class MyClass():
def __init__(self,val1,val2):
self.val1 = val1
self.val2 = val2
def __repr__(self):
return f"{self.val1} and {self.val2}"
def __str__(self):
return self.__repr__()
and create a new object in another cell with
myObject = MyClass(1,2)
myObject is shown in the Window Juyper: Variables but no button Show variable in data viewer appears.
My question: Is there any way to define a custom representation of for example MyObject that the data viewer can recognise and display or are there any packages that would allow the data viewer to pick up for example the repr or str method?

plone.formwidget - Is it possible to set a MasterSelect Field as an AutocompleteFieldWidget?

I am trying to set a MasterSelect field to an AutocompleteFieldWidget.
I'm using AutocompleteFieldWidget from plone.formwidget.autocomplete and the MasterSelectField from plone.formwidget.MasterSelect. The slave field belonging to the MasterSelectField is also a MasterSelectField.
The autocomplete functions as it should (retrieving the values based on input), but the slave field's choices do not change. However, when its not set as an autocomplete, everything works as it should.
Edit:
In my buildout-cache, I looked at widget.py in plone.formwidget.masterselect and tried placing a print statement in getSlaves and that function wasn't getting called. I tried the render function and that wasn't getting called either. Then I placed a print statement in MasterSelectField and that was notgetting called. Setting the field to an Autocomplete widget removes any trace that its a Master Select field.
Edit: In the init.py file in plone.formwidget.masterselect, I placed a print statement in the init function of the MasterSelectField, and the slave widget does print, where as in getSlaves in widget.py it doesn't. This is the output I'm getting from printing in the init and what I should be getting in getSlaves:
({'action': 'vocabulary', 'masterID': 'form-widgets-IMyForm-master_field',
'control_param': 'master_value', 'name': 'IMyForm.slave_field',
'vocab_method': <class 'my.product.vocabulary.SlaveVocab'>},)
I have my interface:
from plone.directives import form
class IMyForm(model.Schema):
form.widget(master_field=AutocompleteFieldWidget)
master_field = MasterSelectField(
title=_(u'Master'),
slave_fields=({'name':'IMyForm.slave_field',
'action':'vocabulary',
'source':MySource,
'control_param':'master_value'
}),
required=True,
)
slave_field = MasterSelectField(title=_(u'Slave Field'),
source=SlaveVocab,
slave_fields=(....
)
required=False,
)
I have my source object for the master field:
class MySource(object):
implements(IQuerySource)
def __init__(self, context):
simple_terms = []
#Query portal catalog for unique indexes, and fill with simple terms
self.vocab = SimpleVocabulary(simple_terms)
def __contains__(self, term):
return self.vocab.__contains__(term)
def getTermByToken(self, token):
return self.getTermByToken(token)
def getTerm(self, value):
return self.getTerm(value)
def search(self, query_string):
return [term for term in self.vocab if query_string in term.title.lower()]
class MySourceBinder(object):
implements(IContextSourceBinder)
def __call__(self, context):
return MySource(context)
My slave field's source is:
class SlaveVocab(object):
grok.implements(IContextSourceBinder)
def __init__(self, **kw):
self.master_value = kw.get('master_value', None)
def __call__(self, context):
if self.master_value is None or self.master_value == "--NOVALUE--"
self.master_value = getattr(context,'master_field',None)
#Still nothing, return empty vocabulary
if self.master_value is None or self.master_value == '--NOVALUE--':
return SimpleVocabulary([])
terms = []
#If not null, building a simple vocabulary to return
return SimpleVocabulary(terms)
I did a print statement in call of the Slave Vocabulary and it was being called, but nothing was being passed in.
I also tried using another widget, ChosenFieldWidget. I get the same results in that it functions as it should, but the slave field's choices do not change. Is it possible to set a master select field to an autocomplete? If so, what am I doing wrong?
Also, I'm using Solgema.fullcalendar and the content type extends the IEventBasic behavior, so I don't have access to using my own form class I would've liked to have used since Solgema seems to render its own forms.
Edit:
I am using Plone 4.3

Storing a variable from a class within a class (tkinter)

I am trying to capture the entry value from the pop up window into a variable that I can access later and manipulate. I need to make a lists of several message box entries to generate questions for the user. However, whenever I try to capture it the variable I assign it to comes up empty.
from tkinter import *
root = Tk()
topFrame = Frame(root)
topFrame.pack()
class popupWindowID (object):
def __init__(self,master):
top=self.top=Toplevel(master)
self.label=Label(top,text="ID")
self.label.pack()
self.entrybox=Entry(top)
self.entrybox.pack()
self.b=Button(top,text='Enter',command=self.cleanup)
self.b.pack()
def cleanup(self): # This cleans it up so it can be overwritten if needed
self.value=str(self.entrybox.get())
self.top.destroy()
class mainWindow(object):
def __init__(self,master):
self.master=master
self.ID=Button(topFrame,text="ID", command=self.popupID)
self.ID.pack(side = TOP)
self.info=Button(topFrame,text="Show Test Information",command=self.entryValue)
self.info.pack(side = TOP)
def popupID(self):
self.ID=popupWindowID(self.master)
self.master.wait_window(self.ID.top)
def entryValue(self):
print(self.ID.value)
m=mainWindow(root)
root.mainloop()

Django-Nonrel with Mongodb listfield

I am trying to implement manytomany field relation in django-nonrel on mongodb. It was suggessted at to:
Django-nonrel form field for ListField
Following the accepted answer
models.py
class MyClass(models.Model):
field = ListField(models.ForeignKey(AnotherClass))
i am not sure where the following goes, it has been tested in fields.py, widgets,py, models.py
class ModelListField(ListField):
def formfield(self, **kwargs):
return FormListField(**kwargs)
class ListFieldWidget(SelectMultiple):
pass
class FormListField(MultipleChoiceField):
"""
This is a custom form field that can display a ModelListField as a Multiple Select GUI element.
"""
widget = ListFieldWidget
def clean(self, value):
#TODO: clean your data in whatever way is correct in your case and return cleaned data instead of just the value
return value
admin.py
class MyClassAdmin(admin.ModelAdmin):
form = MyClassForm
def __init__(self, model, admin_site):
super(MyClassAdmin,self).__init__(model, admin_site)
admin.site.register(MyClass, MyClassAdmin)
The following Errors keep popping up:
If the middle custom class code is used in models.py
name 'SelectMultiple' is not defined
If custom class code is taken off models.py:
No form field implemented for <class 'djangotoolbox.fields.ListField'>
You just need to import SelectMultiple by the sound of it. You can put the code in any of those three files, fields.py would make sense.
Since it's pretty usual to have:
from django import forms
at the top of your file already, you probably just want to edit the code below to:
# you'll have to work out how to import the Mongo ListField for yourself :)
class ModelListField(ListField):
def formfield(self, **kwargs):
return FormListField(**kwargs)
class ListFieldWidget(forms.SelectMultiple):
pass
class FormListField(forms.MultipleChoiceField):
"""
This is a custom form field that can display a ModelListField as a Multiple Select GUI element.
"""
widget = ListFieldWidget
def clean(self, value):
#TODO: clean your data in whatever way is correct in your case and return cleaned data instead of just the value
return value
You probably also want to try and learn a bit more about how python works, how to import modules etc.