1
$\begingroup$

I am currently trying to make a FormPage that plots a Molecule and I try to add a possibility to input a custom Molecule.

Entering the data for a Molecule makes Mathematica crash.

Code: FormPage["m"->Molecule,MoleculePlot@#m&]

An input that works is e.g. Benzene but things like {"H","H"},{Bond[{1,2}]} made Mathematica crash.

It doesn't crash anymore but I get MoleculePlot: Argument Molecule[{"H","H"},{Bond[{1,2}]}] is not a valid molecule.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

You have to specify that the input can be a Mathematica expression. However, {"H", "H"}, {Bond[{1, 2}]} is not really a valid expression, and it must be wrapped in another list {{"H", "H"}, {Bond[{1, 2}]}}. Then you can do it as following:

FormPage["m" -> ToExpression, MoleculePlot[Molecule @@ #m] &]

FormPage

The drawback now is that you have to input "benzene" with the quotemarks. You can solve this by setting m to be a string, then checking if it includes curly braces, and parse it to expression afterwards with ToExpression. And since we are already making a somehow unelegant workaround, let's also manually include the curly brackets.

FormPage["m" -> String,
 MoleculePlot[
   Molecule @@ 
    If[StringContainsQ[#m, "{"], 
     ToExpression["{" <> #m <> "}"], {#m}]] &]

FormPage with parsing expression

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.