Skip to main content

Welcome to WEAS Widget!

PyPI version Docs status Unit test

A widget to visualize and edit atomic structures in Jupyter Notebooks. It uses WEAS (Web Environment For Atomistic Structure) in the backend. Interact with the widget using LLM agents (e.g., LangChain) to guide edits and analysis from your notebook.

Features:

  • Model: space-filling, ball-stick, polyhedral.
  • Supports importing data from ASE and Pymatgen.
  • Edit structure: move, rotate, delete and replace atoms.
  • Supports periodic boundary conditions
  • Animations
  • Isosurfaces
  • Vector fields, e.g., magnetic moments, phonons, ...
  • Fermi surface
  • Lattice planes
  • Integrate with LLMs via LangChain Agent

Demo

Please try the widget in the following links: Colab

🌟 Gallery: projects using weas-widget

📢 Want to Feature Your Project?

If your project uses weas-widget, we’d love to showcase it here!

  • Submit a pull request (PR) with a link to your project.
  • Need a project removed? Open a PR or issue, and we’ll take care of it.

🚀 Let's build a growing community of weas-widget users!

Installation

With pip:

pip install weas-widget

To install the latest version from source, first clone the repository and then install using pip:

git clone https://github.com/superstar54/weas-widget
cd weas-widget
npm install
npm run build
pip install -e .

How to use

from ase.build import molecule
from weas_widget import WeasWidget
atoms = molecule("C2H6SO")
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer

Full documentation at: https://weas-widget.readthedocs.io/en/latest/index.html

Reporting issues

If you encounter any problems, please first update the widget to the latest version.

    pip install weas-widget  --upgrade

If the problem persists, please open a GitHub issue

Features

Select Atoms

  • Pick Selection: Click directly on an atom to select it.
  • Box Selection: Hold the Shift key and drag to select a group of atoms.
  • Lasso Selection: Hold Shift + Alt and drag to select a group of atoms.

Shortcuts for editing selected atoms

Press the keyboard shortcut, and move your mouse.

Operation Shortcut
Move g
Rotate r
Duplicate d

Rotate selected atoms

To rotate around a custom axis, press r then a and click one or two atoms, then press a again to exit axis picking. Rotation defaults to the camera axis through the selection center. One atom sets the rotation center (camera axis), two atoms define the bond axis. The axis is shown with orange crosses and a long orange line (for two atoms). Press a again to exit axis picking and rotate; click an axis atom again to deselect it. Press r then x, y, or z to lock rotation to a world axis (press the same key again to unlock).

Translate along one axis

To translate along one axis, press g then x, y, or z to lock movement. To translate along an atom-defined axis, press g then a and click two atoms, then press a again to exit axis picking. To translate in an atom-defined plane, press g then a and click three atoms, press a again to exit axis picking, then press p for plane or n for normal.

Delete selected atoms

Press the Delete key

Export

  • Export the modified atomic structure to ASE or Pymatgen
atoms = viewer.to_ase()
  • Save image to a path by:
viewer.save_image("/home/xing/filename.png")
  • Download image by:
viewer.download_image("filename.png")

Different styles for the atoms.

Visualizing crystal structures

For a nice visualization of a crystal, show

  • unit cell
  • bonded atoms outside the cell
  • polyhedra
from weas_widget import WeasWidget
viewer1 = WeasWidget()
viewer1.load_example("tio2.cif")
viewer1.avr.model_style = 2
viewer1.avr.boundary = [[-0.1, 1.1], [-0.1, 1.1], [-0.1, 1.1]]
viewer1.avr.show_bonded_atoms = True
viewer1.avr.color_type = "VESTA"
viewer1

Isosurfaces

from ase.build import molecule
from weas_widget import WeasWidget
from ase.io.cube import read_cube_data
volume, atoms = read_cube_data("h2o-homo.cube")
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.iso.volumetric_data = {"values": volume}
viewer.avr.iso.settings = {"positive": {"isovalue": 0.001},
                           "negative": {"isovalue": -0.001, "color": "yellow"}
                           }
viewer

Fermi surface

Requires the optional fermi-surface dependencies (includes seekpath):

pip install "weas-widget[fermi-surface]"
from weas_widget import WeasWidget
viewer = WeasWidget()
viewer.add_fermi_surface_from_bxsf("copper.bxsf", clip_bz=True)
viewer

Magnetic moments

Show the magnetic moments as a vector field.

from ase.build import bulk
from weas_widget import WeasWidget
import numpy as np
atoms = bulk("Fe", cubic=True)
atoms*=[2, 2, 1]
atoms.set_array("moment", np.ones(len(atoms)))
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.model_style = 1
viewer

Phonons

Animate vibrational (phonon) modes (computed with external software).

import numpy as np
from ase.build import bulk
from weas_widget import WeasWidget
atoms = bulk("Fe", cubic=True)
phonon_setting = {"eigenvectors": np.array([[[0, 0], [0, 0],[0.5, 0]],
                                    [[0, 0], [0, 0], [-0.5, 0]]]
                                    ),
        "kpoint": [0, 0, 0], # optional
        "amplitude": 5, # scale the motion of the atoms
        "factor": 1.5, # scale the length of the arrows
        "nframes": 20,
        "repeat": [4, 4, 1],
        "color": "blue",
        "radius": 0.1,
        }
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.phonon_setting = phonon_setting
viewer

Lattice plane

Draw a plane that is defined by the miller indices and distance from the origin or by selecting the atoms.

viewer.avr.lp.add_plane_from_indices(name = "111",
                                     indices = [1, 1, 1],
                                     distance = 4,
                                     scale = 1.0,
                                     color = [0, 1, 1, 0.5])
viewer.avr.lp.build_plane()

Slice 2D

from ase.build import molecule
from weas_widget import WeasWidget
from ase.io.cube import read_cube_data
volume, atoms = read_cube_data("h2o-homo.cube")
viewer = WeasWidget()
viewer.from_ase(atoms)
viewer.avr.model_style = 1
viewer.avr.volume_slice.volumetric_data = {"values": volume}
viewer.avr.volume_slice.settings = {"Slice 1": {"h": 0, "k": 1, "l": 0, "distance": 5.5, "samplingDistance": 0.1 },
                                    "Slice 2": {"h": 1, "k": 1, "l": 0, "distance": 5.5, "samplingDistance": 0.1 },
                                   }
viewer.camera.setting = {"direction": [0.5, 1, 2], "zoom": 1.5}
viewer

Save and restore state

state = viewer.export_state()
viewer.save_state("snapshot.json")

# later
viewer.load_state("snapshot.json")

# or create a viewer directly from a saved state
viewer = WeasWidget.from_state_file("snapshot.json")

Integrate with LangChain Agent

WeasToolkit provides agent-ready tools for inspection and editing (style controls, selection, structure loading, atom edits, camera, measurements, meshes, and more). You can also extend it with your own tools.

from weas_widget import WeasWidget, WeasToolkit
from langchain_openai import ChatOpenAI
from langchain_widget import LangChainWidget
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
import ipywidgets as ipw

load_dotenv()

viewer = WeasWidget()

chat_model = ChatOpenAI(model="gpt-4o-mini")

chat = LangChainWidget(
    chat_model=chat_model,
    tools=WeasToolkit(viewer=viewer),
    title="WEAS Agent Chat",
    system_prompt=(
        "You are a scientific assistant. "
        "Use the available tools to inspect and manipulate the 3D structure."
    ),
)
ipw.VBox([viewer, chat])

See the agent tools reference in the docs: https://weas-widget.readthedocs.io/en/latest/agent_tools.html

Test

Unit test

pytest

End-to-end test

The e2e test is similar to ipywidgets.

For the first time, one needs to install the dependence.

cd tests/notebooks/
yarn install

Then run in a terminal:

yarn start

In another terminal:

yarn test

If the snapshots need to be updated:

yarn test:update

Contact

License

MIT

Metadata

Release files for weas-widget 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weas-widget 0.2.6
File Size Uploaded
weas_widget-0.2.6.tar.gz 329.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for weas-widget 0.2.6
File Interpreter ABI Platform
weas_widget-0.2.6-py3-none-any.whl Python 3 none any Details

Total release size: 674.5 kB

Release files / weas_widget-0.2.6.tar.gz

Download URL weas_widget-0.2.6.tar.gz
Size 329.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9f4524a57ffbbf6060eaa1d22f9c6072155c219f3c81a39da36d9c7ac9dff28b
BLAKE2b-256 checksum
How to use checksums
352339fb0478928f4e7f78ec9e0eb1c8b71a11d7a2fafde9831531c404af128d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / weas_widget-0.2.6-py3-none-any.whl

Download URL weas_widget-0.2.6-py3-none-any.whl
Size 345.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06bec3bfca85f27ae9089da1ff6ab33a97e9604ee70eb009f5ea5b8a2c38bcf4
BLAKE2b-256 checksum
How to use checksums
03ce089287b3a09f1c552d54e27cc07211f2e1fd5301e89e321cabcdc9296d2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.2.6 This release

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.26

2 release files

0.1.25

2 release files

0.1.22

2 release files

0.1.21

2 release files

0.1.19

2 release files

0.1.18

2 release files

0.1.17

2 release files

0.1.16

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page