Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bugs by using Dict for the selected_elements #14

Merged
merged 20 commits into from
Jul 12, 2020
Merged

Conversation

dou-du
Copy link
Contributor

@dou-du dou-du commented May 23, 2020

In order to fix the bugs mentioned in issue #4 , I decide to use a Dict (traitlets) for the selected_elements.

widget.selected_elements = {"H":0, "O":1}

However, there is a issue for the Dict.

#This will not work
widget.selected_elements["H"] = 2

@dou-du
Copy link
Contributor Author

dou-du commented May 23, 2020

@giovannipizzi
Please make a review for this pull request.

Copy link
Contributor

@CasperWA CasperWA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great with some updates on this widget! :)

Although I still have some issues using it to its fullest potential.

@@ -81,25 +81,24 @@ class MCPTableView extends DOMWidgetView {
// http://codebeerstartups.com/2012/12/how-to-improve-templates-in-backbone-js-learning-backbone-js/
tableTemplate = _.template( '<% for (let elementRow of elementTable)'+
' { print("<div class=\'periodic-table-row\'>"); for (let elementName of elementRow)'+
' { if ( (elementName === "") || (elementName == "*" ) ) { %>' +
' { if ( (elementName === "") || (elementName == "*" ) || (elementName == "#" ) ) { %>' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess * is not needed anymore due to the change above from * -> #?

Suggested change
' { if ( (elementName === "") || (elementName == "*" ) || (elementName == "#" ) ) { %>' +
' { if ( (elementName == "") || (elementName == "#" ) ) { %>' +

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used both "#" and "*" to separate lanthanides and actinides group.

Comment on lines +92 to +93
// 'title="state: <% if (selectedElements.includes(elementName)) { i = selectedElements.indexOf(elementName); print(selectedStates[i]);} '+
// 'else if (disabledElements.includes(elementName)){print("disabled");} else {print("unselected");} %>" ><% '+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the commenting out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giovanni suggested to remove the tooltips from the periodic table.

src/widget.ts Outdated Show resolved Hide resolved
src/widget.ts Outdated
Comment on lines 171 to 172
// I have to make some changes, since there is some issue
// for Dict in Traitlets, which cannot triggle the update
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is true. I think you just need to set a default value of {} for the traitlet or something, right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is that if it was already {}, no message is sent back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No only for empty dict. For example newStateList was {"H":1, "O": 2} then

newSateList = {"H": 3, "O": 1}

will not trigger the update in the python end PTable.selected_elements will show {"H":1, "O": 2}

this.model.set('selected_states', newStatesList);
// I have to make some changes, since there is some issue
// for Dict in Traitlets, which cannot triggle the update
this.model.set('selected_elements', {"Du": 0});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? Shouldn't it be set to the actually selected elements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a dirty trick to force the update for the PTable.selected_elements as mentioned above.

Comment on lines +223 to +225
// while (newSelectedElements.length > newSelectedStates.length){
// newSelectedStates.push(0);
// };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the commenting out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for previous code, when using list for the selected_elements and selected_states. For the dictionary, it does not need anymore.

widget_periodictable/periodic_table.py Outdated Show resolved Hide resolved
self.selected_colors = self.selected_colors[:states]


def set_element_state(self, elementName, state):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function is ever used. Hence the functionality of the widget is non-existing at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is import to set the element states.

For example:

PTable.selected_elements["H"] = 2

will not work.

On can use this function to set individual element's state.

@CasperWA
Copy link
Contributor

CasperWA commented Jul 9, 2020

@giovannipizzi
Please make a review for this pull request.

@dou-du you can assign people for review on the right hand side of this page (the "Reviewers" section).

Copy link

@giovannipizzi giovannipizzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments.
I couldn't check the code in detail, though. Can I check it on binder already, I guess?

{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you committing on purpose a run notebook?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just commit to save all change. This is not needed to be committed. I will change it back.

@@ -1,6 +1,6 @@
#!/bin/bash
jupyter labextension install @jupyter-widgets/jupyterlab-manager
pip install -i https://test.pypi.org/simple/ widget-periodictable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to suggest to install from test-pypi?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just use it to test the widget-periodictable package. I have released a new version at PyPi. I will change it to PyPi.

src/widget.ts Outdated
Comment on lines 171 to 172
// I have to make some changes, since there is some issue
// for Dict in Traitlets, which cannot triggle the update

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is that if it was already {}, no message is sent back.

@giovannipizzi
Copy link

I checked on Binder, seems to be working fine!

I think it would be also useful to have a function widget.unselect_element(elementName), but it's not critical for now, there are various ways to achieve so, e.g. disable and reenable the element (even if it's inelegant) or things like

tmp = widget.selected_elements.copy()
tmp.pop(elementName)
widget.selected_elements = tmp

dou-du and others added 2 commits July 9, 2020 18:10
Co-authored-by: Casper Welzel Andersen <[email protected]>
@dou-du
Copy link
Contributor Author

dou-du commented Jul 9, 2020

Thanks @CasperWA and @giovannipizzi

I will have a final checking about the code and approve the pull request.

@dou-du dou-du merged commit 2e623fd into master Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants