EXE.pythonEval: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Deprecated|newvarlink=Using Node and Electron to build Desktop Apps|newvarname=Using Node and Electron to build Desktop Apps}}
EXE.pythonEval(''code''[, ''callback''])
EXE.pythonEval(''code''[, ''callback''])


== Description ==
== Description ==


EXE.pythonEval executes code in the EXE runtime wrapper. It only works in EXE apps. AppStudio's EXE apps work by wrapping your app in a executable wrapper which is written in Python. This function requires you to know Python to use it.
EXE.pythonEval executes code in the [[Make Windows Executables|EXE runtime wrapper]]. It only works in EXE apps. AppStudio's EXE apps work by wrapping your app in a executable wrapper which is written in Python. This function requires you to know Python to use it.


Code has to be properly formatted Python code, following all Python syntax rules. In addition to the base Python language, the following libraries can be used:
Code has to be properly formatted Python code, following all Python syntax rules. In addition to the base Python language, the following libraries can be used:
Line 15: Line 17:
Calls to EXE.pythonEval are processed asynchronously. To return values, put {callback}(value) in your code, and supply the name of the function you want called as ''callback''.
Calls to EXE.pythonEval are processed asynchronously. To return values, put {callback}(value) in your code, and supply the name of the function you want called as ''callback''.


== Example (BASIC) ==
The length of the code string is limited -around 220 characters. However, the return data can be much larger.


== Example ==
Do a simple calculation and return the result
Do a simple calculation and return the result
<pre>
EXE.pythonEval("{callback}(4*4)", "showResult")


Function showResult(res)
<tabber>
  MsgBox res
JavaScript=
End Function
<syntaxhighlight lang="JavaScript">
</pre>
 
== Example (JavaScript) ==
 
<pre>
EXE.pythonEval("{callback}(4*4)", "showResult");
EXE.pythonEval("{callback}(4*4)", "showResult");


Line 34: Line 30:
     NSB.MsgBox(res);
     NSB.MsgBox(res);
}
}
</pre>
</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
EXE.pythonEval("{callback}(4*4)", "showResult")
 
Function showResult(res)
  MsgBox res
End Function
</syntaxhighlight>
</tabber>


== Other examples ==
== Other examples ==
Line 48: Line 54:
</pre>
</pre>


Run a code snippet
Run a code snippet. Put the following code into a TextArea:
 
Put the following code into a TextArea:
<pre>
<pre>
# Code must be in Python.
# Code must be in Python.
Line 60: Line 64:
And execute it as follows:
And execute it as follows:
<pre>EXE.pythonEval(TextArea1.value, "showResult")</pre>
<pre>EXE.pythonEval(TextArea1.value, "showResult")</pre>
Get a list of files in the current directory (BASIC):
<pre>
EXE.pythonEval("{callback}('\r'.join(os.listdir('./')))", "showResult")
Function showResult(res)
  TextArea1.text = res
End Function
</pre>
Get a list of files in the current directory (JavaScript):
<pre>
EXE.pythonEval("{callback}('\\r'.join(os.listdir('./')))", "showResult");
function showResult(res) {
    TextArea1.text = res;
}
</pre>
Read a file. (Also: Send multiple lines of code to Python.) (BASIC)
<pre>
s = "file = open('./package.json')" & vbCRLF
s += "{callback}(file.read())"
EXE.pythonEval(s, "showResult")
</pre>
Read a file. (Also: Send multiple lines of code to Python.) (JavaScript)
<pre>
s = "file = open('./package.json')" + '\n';
s += "{callback}(file.read())";
EXE.pythonEval(s, "showResult");
</pre>


== Output ==
== Output ==

Latest revision as of 14:45, 24 July 2019

This deprecated feature should no longer be used, but is still available for reasons of backwards compatibility.

Please see Using Node and Electron to build Desktop Apps for an alternative way to use this feature.

EXE.pythonEval(code[, callback])

Description

EXE.pythonEval executes code in the EXE runtime wrapper. It only works in EXE apps. AppStudio's EXE apps work by wrapping your app in a executable wrapper which is written in Python. This function requires you to know Python to use it.

Code has to be properly formatted Python code, following all Python syntax rules. In addition to the base Python language, the following libraries can be used:

  • os
  • wx
  • jason
  • yaml

Others may be included in a future release.

Calls to EXE.pythonEval are processed asynchronously. To return values, put {callback}(value) in your code, and supply the name of the function you want called as callback.

The length of the code string is limited -around 220 characters. However, the return data can be much larger.

Example

Do a simple calculation and return the result

EXE.pythonEval("{callback}(4*4)", "showResult");

function showResult(res) {
    NSB.MsgBox(res);
}

EXE.pythonEval("{callback}(4*4)", "showResult")

Function showResult(res)
  MsgBox res
End Function

Other examples

Exit the app

EXE.pythonEval("sys.exit(0)")

Put up a wx Message Box:

EXE.pythonEval("wx.MessageBox('It works!', 'Info', wx.OK | wx.ICON_INFORMATION)", "MessageBox")

Run a code snippet. Put the following code into a TextArea:

# Code must be in Python.
# note how to return results.
a=64
{callback}(a*a)

And execute it as follows:

EXE.pythonEval(TextArea1.value, "showResult")

Get a list of files in the current directory (BASIC):

EXE.pythonEval("{callback}('\r'.join(os.listdir('./')))", "showResult")
Function showResult(res)
  TextArea1.text = res
End Function

Get a list of files in the current directory (JavaScript):

EXE.pythonEval("{callback}('\\r'.join(os.listdir('./')))", "showResult");
function showResult(res) {
    TextArea1.text = res;
}

Read a file. (Also: Send multiple lines of code to Python.) (BASIC)

s = "file = open('./package.json')" & vbCRLF
s += "{callback}(file.read())"
EXE.pythonEval(s, "showResult") 

Read a file. (Also: Send multiple lines of code to Python.) (JavaScript)

s = "file = open('./package.json')" + '\n';
s += "{callback}(file.read())";
EXE.pythonEval(s, "showResult");

Output

Related Items