1
$\begingroup$

I have a problem with a simple InputField of type Expression:

DynamicModule[{inputExpr = {{{1, 2}, {4, 3}}, {{1, 4}, {2, 3}}} , result = ""},
 Column[{Style["Input:", 12, Blue, Editable -> False],
   InputField[Dynamic[inputExpr], Expression, BaseStyle -> {"Input", 12}, 
    ImageSize -> 500],
   Button[Style["Calculate", 12, Red, Bold, Editable -> False],
    result = inputExpr^2, ImageSize -> 500],
   Style["Result:", 12, Red, Editable -> False],
   Framed[Dynamic[result], BaseStyle -> {12, PrintPrecision -> 3}, ImageSize -> 500]}, 
  Alignment -> Left]]

output

When I input a different expression than the default, I get an error when I press the "Calculate" button:

screen recording

What am I doing wrong? How to create a working input field of Expression type? Note, that I need to run it within the free CDF Player, which has some limitations for InputField.

I tried to use the type Boxes instead of Expression (as here), and it works in the full Mathematica, but not in the free CDF Player.

$\endgroup$
8
  • $\begingroup$ An old partial duplicate without a solution: "CDF player InputField list." $\endgroup$ Commented Sep 28, 2022 at 16:06
  • 2
    $\begingroup$ I seems to work if you don't set the BaseStyle to Input. Maybe since Input is a cell style, this triggers some box-related computation to occur. $\endgroup$
    – lericr
    Commented Sep 28, 2022 at 16:17
  • $\begingroup$ Although what "work" means needs to be clarifies. It seems that if the input isn't a valid expression, then it's interpreted as a string. Maybe that's acceptable? $\endgroup$
    – lericr
    Commented Sep 28, 2022 at 16:19
  • $\begingroup$ @lericr Yes, that's acceplable. But I need to run it in the free CDF player. $\endgroup$ Commented Sep 28, 2022 at 16:30
  • 1
    $\begingroup$ Can't comment more time atm but are closely related topics: mathematica.stackexchange.com/q/199972/5478 $\endgroup$
    – Kuba
    Commented Sep 28, 2022 at 18:11

2 Answers 2

1
$\begingroup$

Answering my own question, lericr is correct that the problem appears only when I specify the "Input" style in the BaseStyle of InputField. Removing this makes the code working in the full Mathematica. The immediate source of the problem is the option FormatType -> InputForm included in the "Input" style. Adding the option FormatType -> StandardForm to the BaseStyle of InputField also works:

BaseStyle -> {"Input", 12, FormatType -> StandardForm}

Alternatively, one can use Boxes instead of Expression as in this answer and keep just the "Input" style. This also works in the full Mathematica.

To reproduce the styling of the input cells more closely, one can also add the "Notebook" style and the options ShowCodeAssist -> True, ShowSyntaxStyles -> True as in this answer:

BaseStyle -> {"Notebook", "Input", 12, FormatType -> StandardForm,
              ShowCodeAssist -> True, ShowSyntaxStyles -> True}

As to why all these solutions don't work in the free CDF Player, the following comment by Christopher Wolfram explains the situation:

CDF does not allow input fields because if you could you could create a free version of Mathematica using the CDF player.

I wasn't able to find an information about the limitations of the free CDF/Wolfram Player on the official site, but Szabolcs cites it in a comment under an older question:

Non-numeric input fields are not supported. Avoid InputField[x, String] and InputField[x, Boxes]. InputField[x, Expression] and InputField[x] are restricted to work only with numbers, and InputField[x, Number] works normally.

$\endgroup$
0
$\begingroup$

I appear to have made some progress with the following changes:

(* Make inputExpr a string *)
inputExpr = "{{{1, 2}, {4, 3}}, {{1, 4}, {2, 3}}}"

(* Set the type of InputField to String *)
InputField[
    Dynamic[inputExpr],
    String, (* <--- *)
    BaseStyle -> {"Input", 12},
    ImageSize -> 500
]

(* Change result to the following *)
result = ToExpression[inputExpr]^2

There might be a bug with InputField set to type Expression.

$\endgroup$
1
  • $\begingroup$ Unfortunately, it doesn't work within the free CDF player. A better workaround with Boxes instead of Expression also doesn't work. $\endgroup$ Commented Sep 28, 2022 at 16:09

Your Answer

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

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