FormFunction[formspec,func]
表示一个激活表单,当它被提交时,对根据 formspec 指定的表单所获得的值应用 func.
FormFunction[{"name1"type1,…},func]
表示一个激活表单,其中名为 namei 的字段被解释为类型 typei.
FormFunction[{{"name1",label1}type1,…},func]
使用 labeli 作为名为 namei 的字段的标签.
FormFunction[{namespec1type1default1,…},func]
使用 defaulti 作为由 namespeci 指定的字段的缺省值.
FormFunction[formspec,func,fmt]
指定在云端应用 func 得到的结果应该以格式 fmt 返回.
FormFunction[{formspec1,formspec2,…},func,…]
表示多页面表单,其中连续的 formspeci 可以是应用于目前得到的数值的函数.
FormFunction
字段类型列表 »FormFunction[formspec,func]
表示一个激活表单,当它被提交时,对根据 formspec 指定的表单所获得的值应用 func.
FormFunction[{"name1"type1,…},func]
表示一个激活表单,其中名为 namei 的字段被解释为类型 typei.
FormFunction[{{"name1",label1}type1,…},func]
使用 labeli 作为名为 namei 的字段的标签.
FormFunction[{namespec1type1default1,…},func]
使用 defaulti 作为由 namespeci 指定的字段的缺省值.
FormFunction[formspec,func,fmt]
指定在云端应用 func 得到的结果应该以格式 fmt 返回.
FormFunction[{formspec1,formspec2,…},func,…]
表示多页面表单,其中连续的 formspeci 可以是应用于目前得到的数值的函数.
更多信息和选项
- FormFunction[formspec,func] 显示由 formspec 指定的表单,以及一个“提交”按钮.
- 在 FormFunction[formspec,func] 中,formspec 可以是 FormObject[…].
- FormFunction[formspec,func] 求值以给出 FormFunction[FormObject[…],func],其中 FormObject 包含表单中所有字段的完整规范.
- CloudDeploy[FormFunction[…]] 生成一个包含表单激活版本的云对象.
- FormFunction[formspec,func][] 首先运算 Setting[formspec],给出表单中字段值的关联. 如果未生成验证错误,则对关联应用 func,并返回结果.
- 在评价 FormFunction 的效果之前,FormFunction[formspec,func][<|"namek"->inputk,…|>] 会将给定的输入插入到由 FormFunction 指定的表单.
- FormFunction[…][] 计算为激活表单,在假定给定输入有效的情况下,表单被提交时应用 func 的结果替代.
- 每个字段的 typei 可以是以下任意一种:
-
"form" Interpreter 支持的任意表单 Restricted[…], etc. Interpreter 支持的任意受条件限制的表单等 Interpreter[…] 任意 Interpreter 对象 assoc FormObject 中使用的关联 tfun 应用于字符串的任意函数 - 在由 FormFunction[{formspec1,formspec2,…},…] 表示的多页面表格中,显示由 formspeci 指定的连续多个表格,其中默认情况下最后的表格具有“完成”按钮,而前面的表格具有“下一个”按钮.
- 在 FormFunction[{formspec1,formspec2,…},…] 中,formspec1 后的 formspeci 通常总是纯函数. 它们通过给出从所有前面的 formspeci 得到的所有输入的数值应用于一个关联.
- FormFunction 的总体选项包括:
-
AppearanceRules Automatic 给出总体外观规则的关联 FormLayoutFunction Automatic 生成表单布局的函数 FormProtectionMethod Automatic 防护恶意内容的方法 ImageFormattingWidth $ImageFormattingWidth 输出格式的封装宽度 ImageSize Automatic 输出的整体图像尺寸 PageTheme Automatic 已部署网页的整体主题 - 当在云端被调用时,FormFunction[formspec,func] 默认返回适合网页输出格式的结果.
- FormFunction[formspec,func,fmt] 中的可能结果规范 fmt 有:
-
Automatic 作为尽可能合适的 HTML 或 CloudCDFElement 导出 "HTML" 作为没有动态内容的原始静态 HTML 页面导出 "HTMLThemed" 使用和表单相同的主题作为静态 HTML 导出 "HTMLPaneled" 作为面板中的 HTML 内容导出 "GIF","JPEG","PNG",… 结果的图像 "String" 结果的原始字符串形式 "fmt" ExportForm 支持的任意格式 None 不加改变的传递字符串或字节列表 {None,"type"} 通过所定义的指定 HTTP 响应类型传递 {fmt,opts} 具有选项的格式规范 f 将被应用于 body 的任意函数 - 当在云端被调用时,FormFunction[formspec,func,"fmt"] 等价于 FormFunction[formspec,ExportForm[func[#],"fmt"]&].
- 当在笔记本界面直接调用时,在 FormFunction[formspec,func,fmt] 中的结果规范 fmt 将被忽略.
范例
打开所有单元 关闭所有单元基本范例 (2)
form = FormFunction[{"first" -> "String", "second" -> "Number"}, f]form[]CloudDeploy[form]form[{"first" -> "str", "second" -> "55"}]form[{"first" -> "str"}]FormFunction[{"x" -> "Boolean", "y" -> TemplateIf[TemplateSlot["x"], "Number", "String"]}, foo]范围 (25)
解释器规范 (6)
使用丰富的 Interpreter 规范:
CloudDeploy[FormFunction[{"x" -> Restricted["Integer", {1, 10}]}, func]]Interpreter 头部是可选的,但是对指定 Interpreter 选项或者其他参数很有用:
CloudDeploy[FormFunction[{"x" -> Interpreter["StructuredDate", TimeZone -> 2], "y" -> Interpreter[ToExpression, Positive]}, func]]CloudDeploy[FormFunction[{"x" -> {1, 2, 3}}, func]]CloudDeploy[FormFunction[{"question" -> {"Yes" -> True, "No" -> False}}, func]]CloudDeploy[FormFunction[{"Country" -> EntityList["Country"]}, func]]CloudDeploy[FormFunction[{"Image" :> Map[ImageResize[ExampleData[#], {100, 100}] &, ExampleData["TestImage"][[1 ;; 6]]]}, Composition[EdgeDetect, First], "PNG"]]CloudDeploy[FormFunction[{"x" -> {"A" -> 1, "B" -> 2, "C" -> 3}}, func]]默认值 (3)
FormFunction[{"first" -> "String", "second" -> "Number" -> 3}, f]["first" -> "str"]FormFunction[{"first" -> "String", "second" -> <|"Interpreter" -> "Number", "Required" -> False|>}, f]["first" -> "str"]使用 RuleDelayed 每次计算一个新的默认值:
FormFunction[{"name" -> "String", "date" -> <|"Interpreter" -> "DateTime", "Default" :> DateObject[]|>}, f][<|"name" -> "Bob", "date" -> None|>]输入 (2)
标签 (1)
CloudDeploy[FormFunction[{"x", "First Name"} -> "String", Identity]]可以使用 RuleDelayed 并且返回任意表达式:
CloudDeploy[FormFunction["pc" -> <|"Label" :> "PC: " <> $MachineID|>, Identity]]CloudDeploy[FormFunction[
"pc" -> <|"Label" :> GeoGraphics[
$GeoLocation,
GeoRange -> Quantity[2, "Miles"]
]|>,
Identity
]]使用 None 删除标签:
CloudDeploy[FormFunction["Query" -> <|"Label" -> None, "Hint" -> "Search"|>, Identity]]CloudDeploy[FormFunction[{
"First" -> <|"Label" -> None, "Hint" -> "First Name"|>,
"Last" -> <|"Label" -> None, "Hint" -> "Last Name"|>
}, Identity]]外观 (4)
CloudDeploy[FormFunction[{"key", "Label"} -> "Integer", g]]CloudDeploy[FormFunction[{"Number", [image]} -> "Number", g]]CloudDeploy[FormFunction[{"x" -> <|"Interpreter" -> Restricted["Integer", {1, 10}], "Control" -> InputField, "Help" -> "help text", "Hint" -> "write something"|>}, func]]使用 RuleDelayed 计算在云端的表达式:
CloudDeploy[FormFunction[{"x" -> <|"Label" -> "Where are you?", "Help" :> GeoGraphics[$GeoLocation]|>}, func]]CloudDeploy[FormFunction[{"x" -> "String", "text", Graphics3D[Sphere[]], "y" -> "Integer"}, func]]控制 (7)
CloudDeploy[FormFunction[{"x" -> "Color"}, func]]CloudDeploy[FormFunction[{"x" -> "Image"}, func]]CloudDeploy[FormFunction[{"x" -> "Boolean"}, func]]CloudDeploy[FormFunction[{"x" -> <|"Interpreter" -> Restricted["Integer", {1, 10}], "Control" -> Slider|>}, func]]CloudDeploy[FormFunction[FormObject[<|"x" -> <|"Interpreter" -> {"A" -> 1, "B" -> 2, "C" -> 3}, "Control" -> RadioButtonBar|>|>], func]]CloudDeploy[FormFunction[FormObject[<|"x" -> <|"Interpreter" -> {Plot[Sin[x], {x, 0, 10}, ImageSize -> Small] -> 1, Graphics3D[Sphere[], ImageSize -> Small] -> 2, EntityValue[Entity["Species", "Species:FelisCatus"], EntityProperty["Species", "Image"]] -> 3}, "Control" -> RadioButtonBar|>|>], func]]CloudDeploy[FormFunction[{"x" -> <|"Interpreter" -> "String", "Control" -> Function[Tooltip[InputField[##], EntityValue[Entity["Species", "Species:FelisCatus"], EntityProperty["Species", "Image"]]]]|>}, func]]动态表单 (1)
包含 TemplateSlot 的表单的部分不被计算,直到 TemplateSlot[key] 中提到的键被绑定到合适的值:
FormFunction[{{"name", "what's your name?"} -> "GivenName", "gender" -> <|"Interpreter" -> {"Male", "Female"}, "Input" -> TemplateIf[TemplateSlot["name"][[-1, -1]] == "male", "Male", "Female"]|>}][]多页面表单 (1)
FormFunction 支持跨越了几个页面的表单. 信息栏取决于前面页面中的值:
FormFunction[{{{"rooms" , "number of rooms"} -> "Integer"}, Function[Table[{"room" <> ToString[i], "guests in room" <> ToString[i]} -> "Integer", {i, #rooms}]]}][]选项 (6)
AppearanceRules (1)
CloudDeploy[FormFunction["Start" -> "Country", func, AppearanceRules -> <|"Title" -> "Where do you want to go?", "Description" -> "Enter a country and we will find you an accomodation"|>]]CloudDeploy[FormFunction["Product" -> {"Flyers", "Business cards", "Leflets"}, func, AppearanceRules -> <|"Title" -> [image], "Description" -> "What do you want to print today?"|>]]CloudDeploy[FormFunction[{"First" -> "String", "Second" -> "String"}, func, AppearanceRules -> <|"ItemLayout" -> "Vertical"|>]]CloudDeploy[FormFunction[{"First" -> <|"Help" -> "Insert a value"|>, "Second" -> <|"Help" -> "Insert a value"|>}, func, AppearanceRules -> <|"ItemLayout" -> "Inline"|>]]CloudDeploy[FormFunction["Product" -> {"Flyers", "Business cards", "Leflets"}, func, AppearanceRules -> <|"SubmitLabel" -> "Buy now!"|>]]FormLayoutFunction (1)
PageTheme (4)
CloudDeploy[FormFunction[{"age" -> "Number"}, First, PageTheme -> "White"]]CloudDeploy[FormFunction[{"age" -> "Number"}, First, PageTheme -> "Blue"]]CloudDeploy[FormFunction[{"age" -> "Number"}, First, PageTheme -> "Black"]]CloudDeploy[FormFunction[{"age" -> "Number"}, First, PageTheme -> "Red"]]应用 (2)
CloudDeploy[FormFunction[{"from" -> "Airport", "to" -> "Airport"}, GeoGraphics[GeoPath[{GeoPosition[#from], GeoPosition[#to]}]]&, "PNG"]]CloudDeploy @ FormFunction[{
"city" -> <|"Interpreter" -> "City", "Input" -> "NYC"|>,
"years" -> <|
"Input" -> "2000 - 2010",
"Interpreter" -> Restricted[
DelimitedSequence[
Restricted["Integer", {1900, 2014}],
"-"
],
{1, 2}
]|>
},
Module[{data, range},
range = Range @@ PadRight[Sort[#years], 2, First[#years]];
data = EntityValue[
#city,
Table[
EntityProperty[
"City",
"Population",
{"Date" -> DateObject[{year}]}
],
{year, range}
]
];
Grid @ Transpose[{range, data}]
]
&,
"HTML",
AppearanceRules -> <|
"Title" -> "Demographic data",
"Description" -> "Enter any city and a date range."
|>
]属性和关系 (8)
FormFunction 被设计成可以与 APIFunction 交换:
CloudDeploy[FormFunction @@ APIFunction["x" -> "String", Identity]]CloudDeploy[APIFunction @@ FormFunction["x" -> "String", Identity]]FormFunction 的第一个参数可以以完整的 FormObject 给出:
FormFunction[FormObject["x" -> "Integer"], Identity]计算 FormFunction,使得第一个变量变成完整的 FormObject:
FormFunction["x" -> "String", Identity]//InputFormFormFunction 的单个域使用 Interpreter 转换 Wolfram 语言表达式中的输入:
ff1 = FormFunction["x" -> "Country", #x&]ff2 = FormFunction["x" -> Interpreter["Country"], #x&]与直接应用 Interpreter 相同:
ff1["x" -> "france"] === ff2["x" -> "france"] === Interpreter["Country"]["france"]AnySubset 可用于产生一个控件,其中可以挑选多于一个的选项:
CloudDeploy[FormFunction[
{"choose one or more" -> AnySubset[{1, 2, 3}]}]]RepeatingElement 可用于创建可添加或者删除的信息栏群组:
CloudDeploy[FormFunction["field" -> RepeatingElement["String", {2, {1, 5}}]]]CompoundElement 可用于创建信息栏的可扩展表格:
CloudDeploy[FormFunction["names" -> RepeatingElement[CompoundElement[<|"first" -> "String", "last" -> "String"|>]]]]可能存在的问题 (1)
Interpreter["String"] 接受空白字符串. 但是,FormFunction 不接受空白字符串,其中,空白字符串和空白字段被同等对待:
Interpreter["String"][""]form = FormFunction[{"a string" -> "String"}, f];
form[{"a string" -> ""}]巧妙范例 (3)
CloudDeploy[FormFunction[{"Website" -> <|"Interpreter" -> "SemanticURL", "Input" -> "Wikipedia"|>}, Composition[HTTPRedirect, First], AppearanceRules -> <|"Title" -> "WebGo!", "Description" -> "Write the name of any website, and we will take you there!", "SubmitLabel" -> "Go!"|>]]CloudDeploy[FormFunction[{"image" -> "Image", "filter" -> ImageEffect[]}, ImageEffect[#image, #filter]&, "JPEG", AppearanceRules -> {"Title" -> "Wolf(g)ram"}]]CloudDeploy[
FormFunction[
"Input" -> <|"Input" -> "Hello World!", "Label" -> None|>,
SpeechSynthesize[#Input]&,
AppearanceRules -> <|
"Title" -> "Text to Speech",
"Description" -> "Write to me, I'll speak to you!"
|>
], "WolframSpeak"
]技术笔记
-
▪
- 创建高级网页表单
相关的工作流程
- 创建包含内容以及输入字段的网页表单 ▪
- 制作带有滑块和其他控件的表单 ▪
- 提供表单的初始数据 ▪
- 构建多页表单 ▪
- 创建图像处理网站 ▪
- 使用模板创建网页 ▪
- 设置将图像作为输入的表单
文本
Wolfram Research (2014),FormFunction,Wolfram 语言函数,https://reference.wolfram.com/language/ref/FormFunction.html (更新于 2015 年).
CMS
Wolfram 语言. 2014. "FormFunction." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2015. https://reference.wolfram.com/language/ref/FormFunction.html.
APA
Wolfram 语言. (2014). FormFunction. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/FormFunction.html 年
BibTeX
@misc{reference.wolfram_2026_formfunction, author="Wolfram Research", title="{FormFunction}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/FormFunction.html}", note=[Accessed: 08-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_formfunction, organization={Wolfram Research}, title={FormFunction}, year={2015}, url={https://reference.wolfram.com/language/ref/FormFunction.html}, note=[Accessed: 08-November-2026]}