Javascript/python

How can we pass a robin variable to our javascript code or python code??

Hello @Yash_bitla99, Robin variables can be passed via the corresponding actions, following the Text Concatenation methods.

Feel free to refer to the example below:

#Create list of random numbers
set List to ''
loop i from 0 to 9 step 1
    Variables.RandomNumber MinimumValue:0 \
    MaximumValue:100 RandomNumber=> RandomNumber
    set List to RandomNumber+','+List
end
Variables.RandomNumber MinimumValue:0 \
    MaximumValue:100 RandomNumber=> RandomNumber
    set List to List+RandomNumber

#Execute Python
System.RunPython PythonCode: '''
max = max('''+List+''')
print(max)
''' \
ModuleFolderPaths:'' \
ScriptOutput=> ScriptOutput ScriptError=> ScriptError

#Execute Javascript
System.RunJavascript JavascriptCode:'''
var max = Math.max('''+List+''');
WScript.Echo(max);
''' \
ScriptOutput=> ScriptOutput ScriptError=> ScriptError

#Execute Javascript on Webpage
WebAutomation.LaunchAutomationBrowser \
Url: 'https://robin-language.org/' \
WindowState:WebAutomation.BrowserWindowState.Normal\
ClearCache:False ClearCookies:False \
CustomUserAgentString:'' BrowserInstance=> Browser
WebAutomation.ExecuteJavascript BrowserInstance: Browser \
Javascript:'''function ExecuteScript() {
var max = Math.max('''+List+''');
return max;}''' \
Result=> Result
WebAutomation.CloseWebBrowser \
BrowserInstance: Browser

#Display results on the Console
Console.Write Message: '''
Generated random numbers list: ['''+List+''']
'''
Console.Write \
Message: 'Python output: '+ScriptOutput+' '+ScriptError
Console.Write \
Message: 'Javascript output: '+ScriptOutput+' '+ScriptError
Console.Write \
Message: 'Javascript on Webpage output: '+Result+'''
'''

Now suppose that you have the script already built and stored into a file or as a Text variable in Robin Editor. Any Robin variables that you wish to pass on the script can be inserted via the Text.Replace action.

set PythonCode to '''
max = max(RobinVariable)
print(max)'''

#Create list of random numbers
set List to ''
loop i from 0 to 9 step 1
    Variables.RandomNumber MinimumValue:0 \
    MaximumValue:100 RandomNumber=> RandomNumber
    set List to RandomNumber+','+List
end
Variables.RandomNumber MinimumValue:0 \
    MaximumValue:100 RandomNumber=> RandomNumber
    set List to List+RandomNumber

#Insert the variables to the already existing Script
Text.Replace Text: PythonCode TextToFind: 'RobinVariable' \
ReplaceWith: List \
IsRegEx:False IgnoreCase:False \
ActivateEscapeSequences:False Result=> PythonCode

#Execute Python Script
System.RunPython PythonCode: PythonCode \
ModuleFolderPaths:'' \
ScriptOutput=> ScriptOutput ScriptError=> ScriptError

#Display results on the Console
Console.Write Message: '''
Generated random numbers list: ['''+List+''']
'''
Console.Write \
Message: 'Python output: '+ScriptOutput+' '+ScriptError

3 Likes

@jokoum, very nice! Thank you.
Regards,
burque505

2 Likes

Thanks alot, this will help me a lot.

1 Like