Exemplo n.º 1
0
 def update_message(self, new_store_contents, *args):
     try:
         self.update_contents(new_store_contents, *args)
     except Exception as exception:
         self.logger.error(
             f"Callback error.",
             exc_info=True,
             extra={"store_contents": new_store_contents},
         )
         error_header = (
             "An error was encountered when trying to load this component, "
             "please report this if it seems like a bug, thank you!")
         # TODO: add GitHub Issue badge to error message box
         return MessageContainer(
             [
                 MessageHeader("Error"),
                 MessageBody([
                     html.Div(error_header),
                     dcc.Markdown("> {}".format(traceback.format_exc())),
                 ]),
             ],
             kind="danger",
         )
     else:
         return html.Div()
Exemplo n.º 2
0
        def update_graph(figure):
            if figure is None:
                raise PreventUpdate
            elif figure == "too_many_elements":
                search_error = (MessageContainer(
                    [
                        MessageBody(
                            dcc.Markdown(
                                "Pourbaix diagrams may only be calculated for materials "
                                "with {} or fewer non-OH elements".format(
                                    SUPPORTED_N_ELEMENTS)))
                    ],
                    kind="warning",
                ), )
                return search_error

            else:
                plot = [
                    dcc.Graph(
                        figure=figure,
                        config={
                            "displayModeBar": False,
                            "displaylogo": False
                        },
                    )
                ]
                return plot
Exemplo n.º 3
0
        def run_robocrys_analysis(new_store_contents):

            print("Robocrys callback fired")

            struct = self.from_data(new_store_contents)

            try:

                condenser = StructureCondenser()
                describer = StructureDescriber()

                condensed_structure = condenser.condense_structure(struct)

                description = describer.describe(condensed_structure)

            except Exception as exc:

                description = str(exc)

            return MessageContainer(
                MessageBody([
                    f"{description} – ",
                    html.A(
                        f"🤖 robocrys v{robocrys_version}",
                        href=
                        "https://github.com/hackingmaterials/robocrystallographer",
                        style={"white-space": "nowrap"},
                    ),
                ]),
                kind="dark",
            )
Exemplo n.º 4
0
    def update_contents(self, new_store_contents):

        struct = self.from_data(new_store_contents)

        condenser = StructureCondenser()
        describer = StructureDescriber()

        condensed_structure = condenser.condense_structure(struct)

        description = describer.describe(condensed_structure)

        return MessageContainer(MessageBody([
            f"{description} – ",
            html.A(
                f"🤖 robocrys v{robocrys_version}",
                href="https://github.com/hackingmaterials/robocrystallographer",
                style={"white-space": "nowrap"},
            ),
        ]),
                                kind="dark")
Exemplo n.º 5
0
        def load_contents(panel_n_clicks, store_contents, *args):
            """
            Only update panel contents if panel is open by default, to speed up
            initial load time.
            """
            panel_initially_open = args[-1]
            # if the panel outputs data, we have to make sure callbacks are fired
            # regardless of if the panel is open or not
            if (not self.has_output) and ((panel_n_clicks is None) or
                                          (panel_initially_open is None)):
                raise PreventUpdate
            if not store_contents:
                return html.Div(), html.Div()

            try:
                return self.update_contents(store_contents,
                                            *args[:-1]), html.Div()
            except Exception as exception:
                self.logger.error(
                    f"Callback error.",
                    exc_info=True,
                    extra={"store_contents": store_contents},
                )
                error_header = (
                    "An error was encountered when trying to load this component, "
                    "please report this if it seems like a bug, thank you!")
                # TODO: add GitHub Issue badge to error message box
                return (
                    html.Div(),
                    MessageContainer(
                        [
                            MessageHeader("Error"),
                            MessageBody([
                                html.Div(error_header),
                                dcc.Markdown(f"> {traceback.format_exc()}"),
                            ]),
                        ],
                        kind="danger",
                    ),
                )
Exemplo n.º 6
0
    def container_layout(self, state=None, structure=None) -> html.Div:
        """
        :return: Layout defining transformation and its options.
        """

        container = MessageContainer(
            [
                MessageHeader(
                    html.Div([
                        self._sub_layouts["enable"],
                        html.Span(
                            self.title,
                            style={
                                "vertical-align": "middle",
                                "margin-left": "1rem",
                            },
                        ),
                    ])),
                MessageBody([
                    Columns([
                        Column([
                            self._sub_layouts["description"],
                            html.Br(),
                            html.Div(
                                self.options_layouts(state=state,
                                                     structure=structure)),
                            html.Br(),
                            self._sub_layouts["message"],
                        ])
                    ])
                ]),
            ],
            kind="dark",
            id=self.id("container"),
        )

        return container
Exemplo n.º 7
0
        def run_transformations(*args):

            # do not update if we don't have a Structure to transform
            if not args[-2]:
                raise PreventUpdate

            user_visible_transformations = args[-1]
            struct = self.from_data(args[-2])

            # for debug
            # print("input struct", struct)

            errors = []

            transformations = []
            for transformation in args[:-2]:
                if transformation:
                    transformations.append(transformation)

            if not transformations:
                return struct  # , html.Div()

            for transformation_data in transformations:

                # following our naming convention, only apply transformations
                # that are user visible
                # TODO: this should be changed
                if (f"{transformation_data['@class']}Component"
                        in user_visible_transformations):

                    struct, error = apply_transformation(
                        transformation_data, struct)

                    if error:
                        errors += error

            if not errors:
                error_msg = html.Div()
            else:
                errors = [
                    dcc.Markdown(
                        "Crystal Toolkit encountered an error when trying to "
                        "applying your chosen transformations. This is usually "
                        "because either the input crystal structure is not "
                        "suitable for the transformation, or the choice of "
                        "transformation settings is not appropriate. Consult "
                        "the pymatgen documentation for more information.  \n"
                        ""
                        "If you think this is a bug please report it.  \n"
                        "")
                ] + errors
                error_msg = html.Div([
                    MessageContainer(
                        [
                            MessageHeader("Error applying transformations"),
                            MessageBody(errors),
                        ],
                        kind="danger",
                    ),
                    html.Br(),
                ])

            # for debug
            # print("transformed struct", struct)

            return struct  # , error_msg