1

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.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • An old partial duplicate without a solution: "CDF player InputField list." – Alexey Popkov Sep 28 '22 at 16:06
  • 2
    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. – lericr Sep 28 '22 at 16:17
  • 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? – lericr Sep 28 '22 at 16:19
  • @lericr Yes, that's acceplable. But I need to run it in the free CDF player. – Alexey Popkov Sep 28 '22 at 16:30
  • I haven't used the CDF player. Is it restricted to "read only" usage, i.e. one can't input new Mathematica expressions? If so, maybe this has more to do with constraints being imposed in the CDF player than with the behavior of InputField? – lericr Sep 28 '22 at 16:48
  • @lericr Yes, now I understand that the problem is with the limitations of the free version. OK, without Player Pro it is impossible to create even InputField of the String type. So I will accept an answer showing at least how to achieve my goal with the full Mathematica (I wish to have the "Input"-like automatic formatting withing the InputField). – Alexey Popkov Sep 28 '22 at 16:56
  • 1
    Can't comment more time atm but are closely related topics: https://mathematica.stackexchange.com/q/199972/5478 – Kuba Sep 28 '22 at 18:11
  • @Kuba I've found that setting FormatType -> StandardForm also solves the problem, see my answer. I'm not completely sure whether the default option FormatType -> InputForm of the "Input" style is a bug or not? – Alexey Popkov Sep 29 '22 at 02:38

2 Answers2

1

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.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
0

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.

Shredderroy
  • 5,249
  • 17
  • 26
  • Unfortunately, it doesn't work within the free CDF player. A better workaround with Boxes instead of Expression also doesn't work. – Alexey Popkov Sep 28 '22 at 16:09