{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"params":[]},"next":{"description":"","pages":[]},"title":"Import","type":"basic","slug":"import","excerpt":"","body":"# Introduction\n\nJulia modules may be imported from Javascript using a syntax similar to that used by *[common.js](http://requirejs.org/docs/commonjs.html)*.  Upon import of a Julia package, a Javascript object is created with function objects corresponding to the exported functions declared in the module declared by the Julia package.\n# Julia Dict to JSON to Javascript\n\nThe following example illustrates the concept:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\nvar juliaJSON = julia.import('JSON');\\n\\n// Create a JRef of a Julia Dict then export that JSON of\\n// the Dict, but Julia exports this as a string, so then\\n// use JSON.parse on the output.\\n\\nvar x = julia.eval('Dict(\\\"a\\\" => \\\"b\\\")');\\nvar y = juliaJSON.json(x);\\n\\nconsole.log(JSON.parse(y));\\t\",\n      \"language\": \"javascript\",\n      \"name\": \"DictToJSON.js\"\n    }\n  ]\n}\n[/block]\n# The *functionNames* variable\n\nIn addition to all of the functions exported from the Julia package, being equated to Javascript functions, the *functionNames* variable is an array of strings of all of names of those functions and can be seen using the following example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\nvar JuMP = julia.import('JuMP');\\n\\nconsole.log(JSON.stringify(JuMP.functionNames,null,2));\",\n      \"language\": \"javascript\",\n      \"name\": \"FunctionNames.js\"\n    }\n  ]\n}\n[/block]\n## Output\n\n    [\n      \":::at:::LinearConstraint\",\n      \"@LinearConstraints\",\n      \"@QuadConstraint\",\n\n    ...\n\n      \"setLower\",\n      \"setName\",\n      \"setObjective\",\n      \"setObjectiveSense\",\n      \"setPrintHook\",\n      \"setSolutionValue!\",\n      \"setSolveHook\",\n      \"setSolver\",\n      \"setUpper\",\n      \"setValue\",\n      \"solve\",\n      \"writeLP\",\n      \"writeMPS\"\n    ]\n\n# Caveat for Macro Functions\nNote how macro names are also included, however there are 2 things to remember here.  First, the character **@** is illegal as a field identifier in Javascript, so referring to the example above, **@LinearConstraint** could be invoked using the following `JuMP['@LinearConstraint'](...)`, but currently the effect is not what is expected; a quote block is returned rather than an inline expansion and evaluation.  Thus, macros invoked in this way will be seldom used, nevertheless, they are included for completeness.\n\n# Example: using JuMP\n\nExtending the above example, and taking from the [JuMP simple example](http://jump.readthedocs.org/en/release-0.9/quickstart.html#simple-example), and noting the use of `julia.eval(...)` when invoking macros, JuMP can be used as follows:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\nvar JuMP = julia.import('JuMP');\\nvar m = julia.eval('m = JuMP.Model()');\\nvar x = julia.eval('[email protected](m,0 <= x <= 2)');\\nvar y = julia.eval('[email protected](m,0 <= y <= 30)');\\n\\njulia.eval('[email protected](m,Max, 5x + 3*y)');\\njulia.eval('[email protected](m,1x + 5y <= 3.0)');\\n\\nvar status = JuMP.solve(m);\\n\\nconsole.log('Objective value: ',JuMP.getObjectiveValue(m));\\nconsole.log('X value: ',JuMP.getValue(x));\\nconsole.log('Y value: ',JuMP.getValue(y));\\n\",\n      \"language\": \"javascript\",\n      \"name\": \"using_JuMP.js\"\n    }\n  ]\n}\n[/block]\n## Output\n\n    Objective value:  10.6\n    X value:  2\n    Y value:  0.2","updates":[],"order":7,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"566e55c12c1e760d0030c575","category":{"sync":{"isSync":false,"url":""},"pages":["566e55c12c1e760d0030c56e","566e55c12c1e760d0030c56f","566e55c12c1e760d0030c570","566e55c12c1e760d0030c571","566e55c12c1e760d0030c572","566e55c12c1e760d0030c573","566e55c12c1e760d0030c574","566e55c12c1e760d0030c575","566e55c12c1e760d0030c576","566e55c12c1e760d0030c577","566e55c12c1e760d0030c578","566e55c12c1e760d0030c579","566e55c12c1e760d0030c57a","566e55c12c1e760d0030c57b","566e55c12c1e760d0030c57c","566e55c12c1e760d0030c57d","566e55c12c1e760d0030c57e","566e55c12c1e760d0030c57f","566e55c12c1e760d0030c580","566e55c12c1e760d0030c581","566e55c12c1e760d0030c582","566e55c12c1e760d0030c583","566e55c12c1e760d0030c584","566e55c12c1e760d0030c585","566e55c12c1e760d0030c586","566e55c12c1e760d0030c587","566e55c12c1e760d0030c588","566e5640c15c8f0d000ee863"],"title":"Documentation","slug":"documentation","order":0,"from_sync":false,"reference":false,"_id":"566e55c12c1e760d0030c56d","__v":2,"createdAt":"2014-10-17T03:34:44.886Z","project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c"},"version":{"version":"1.2.3","version_clean":"1.2.3","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["566e55c12c1e760d0030c56d"],"_id":"566e55c02c1e760d0030c56c","createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","project":"54408e54309354080070a896","__v":1},"createdAt":"2015-05-10T01:51:37.102Z","user":"544083bee239230800071bef","project":"54408e54309354080070a896","githubsync":"","__v":0}
# Introduction Julia modules may be imported from Javascript using a syntax similar to that used by *[common.js](http://requirejs.org/docs/commonjs.html)*. Upon import of a Julia package, a Javascript object is created with function objects corresponding to the exported functions declared in the module declared by the Julia package. # Julia Dict to JSON to Javascript The following example illustrates the concept: [block:code] { "codes": [ { "code": "var julia = require('node-julia');\nvar juliaJSON = julia.import('JSON');\n\n// Create a JRef of a Julia Dict then export that JSON of\n// the Dict, but Julia exports this as a string, so then\n// use JSON.parse on the output.\n\nvar x = julia.eval('Dict(\"a\" => \"b\")');\nvar y = juliaJSON.json(x);\n\nconsole.log(JSON.parse(y));\t", "language": "javascript", "name": "DictToJSON.js" } ] } [/block] # The *functionNames* variable In addition to all of the functions exported from the Julia package, being equated to Javascript functions, the *functionNames* variable is an array of strings of all of names of those functions and can be seen using the following example: [block:code] { "codes": [ { "code": "var julia = require('node-julia');\nvar JuMP = julia.import('JuMP');\n\nconsole.log(JSON.stringify(JuMP.functionNames,null,2));", "language": "javascript", "name": "FunctionNames.js" } ] } [/block] ## Output [ "@LinearConstraint", "@LinearConstraints", "@QuadConstraint", ... "setLower", "setName", "setObjective", "setObjectiveSense", "setPrintHook", "setSolutionValue!", "setSolveHook", "setSolver", "setUpper", "setValue", "solve", "writeLP", "writeMPS" ] # Caveat for Macro Functions Note how macro names are also included, however there are 2 things to remember here. First, the character **@** is illegal as a field identifier in Javascript, so referring to the example above, **@LinearConstraint** could be invoked using the following `JuMP['@LinearConstraint'](...)`, but currently the effect is not what is expected; a quote block is returned rather than an inline expansion and evaluation. Thus, macros invoked in this way will be seldom used, nevertheless, they are included for completeness. # Example: using JuMP Extending the above example, and taking from the [JuMP simple example](http://jump.readthedocs.org/en/release-0.9/quickstart.html#simple-example), and noting the use of `julia.eval(...)` when invoking macros, JuMP can be used as follows: [block:code] { "codes": [ { "code": "var julia = require('node-julia');\nvar JuMP = julia.import('JuMP');\nvar m = julia.eval('m = JuMP.Model()');\nvar x = julia.eval('[email protected](m,0 <= x <= 2)');\nvar y = julia.eval('[email protected](m,0 <= y <= 30)');\n\njulia.eval('[email protected](m,Max, 5x + 3*y)');\njulia.eval('[email protected](m,1x + 5y <= 3.0)');\n\nvar status = JuMP.solve(m);\n\nconsole.log('Objective value: ',JuMP.getObjectiveValue(m));\nconsole.log('X value: ',JuMP.getValue(x));\nconsole.log('Y value: ',JuMP.getValue(y));\n", "language": "javascript", "name": "using_JuMP.js" } ] } [/block] ## Output Objective value: 10.6 X value: 2 Y value: 0.2