Livecode and Python?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Livecode and Python?

Post by M-A Kuttner » Mon Dec 12, 2022 7:50 pm

Hi all. I've got a request to see if I can build a front end in LiveCode that can pass lines of JSON to an emulator that's built in Python. Is that even a thing that can be done? Anyone have any experience in such alchemy?
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Livecode and Python?

Post by stam » Tue Dec 13, 2022 3:31 am

I’m no expert - but would presume the python app has some sort of interface to accept external data - REST API? CLI? File based? Something else?

If it’s one of the first 3 for sure you can do this in livecode. That would be my starting point anyway…

Let us know how you get on!
Stam

M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Re: Livecode and Python?

Post by M-A Kuttner » Tue Dec 13, 2022 4:16 am

Thanks Stam. That gives me somewhere to start.
Cheers
M-A
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Livecode and Python?

Post by AndyP » Tue Dec 13, 2022 9:00 am

This is easy with LiveCode...as usual!

Here is a fully working simple example.

put this in a button

Code: Select all

on mouseUp pMouseButton
   
   --place python file in same folder as the LIveCode file
   getStackPath
   
   -- name of the python file to interface with
   put "add.py" into tPythonFile
   
   --data to be sent to the python file script
   put 5 into p1
   put 23 into p2
   
   -- we don't want the consule window so hide it
   set the hideConsoleWindows to true
   
   -- shell out to the command line and pass the variabled to the python script
   -- then pass the print function result into a variable in LiveCode
   put shell("py " && tPythonFile && p1 && p2) into tResult
   
   --tResult contains the returned print output from python
   answer tResult   
   
end mouseUp

command getStackPath
   
   -- returns the path for this stack and places the
   -- result in the variable tStackFolder
   
   local tStackFile, tStackFolder
   put the effective filename of this stack into tStackFile
   set the itemDelimiter to "/"
   put item 1 to -2 of tStackFile into tStackFolder
   set the defaultFolder to tStackFolder
   
end getStackPath




then save this below as add.py into the same folder as your livecode file

Code: Select all

import sys

pNum1 = sys.argv[1]
pNum2 = sys.argv[2]

sum= int(pNum1) + int(pNum2)
print(sum)

now go here and look at this tutorial

https://www.tutorialspoint.com/python/p ... uments.htm

Have fun
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

M-A Kuttner
Posts: 49
Joined: Mon Apr 03, 2017 3:55 pm
Location: Nova Scotia, Canada

Re: Livecode and Python?

Post by M-A Kuttner » Tue Dec 13, 2022 4:24 pm

Hey, thank you very much Andy! Greatly appreciated!

M-A
Hypertalk developer on and off since the days of SuperCard. Currently using LC to do rapid prototyping for a new kids' toy.

liveme
Posts: 240
Joined: Thu Aug 27, 2015 5:22 pm
Location: down under

Re: Livecode and Python?

Post by liveme » Fri Aug 25, 2023 5:03 am

Great, I've just tested it in a terminal and its super cool that it can work in this way !! :D

I'm wondering, what are some method names in order to pass some values to a python or julia or some other language
without opening a console ahead ?

*Can livecode do so [without opening a terminal before] ...with all other language/programs (ie julia, lua, etc) ?
- or is it something that needs specific features coded for each of those application - that is not part of livecode ?

**just learning dev by myself!
Thanks for any lights...
:idea: :idea: :)

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Livecode and Python?

Post by stam » Fri Aug 25, 2023 7:40 pm

Not sure if I'm answering your question or if you knew this already, but there is the do as alternateLanguage command (details in dictionary)
  • On www: do <script> as "javascript"
  • On macOS: do <script> as "AppleScript" (one caveat that was recently discussed is that file paths in AppleScript need to be absolute, not relative file paths, but otherwise works very well and gives you hooks into system wide APIs that can be driven with AppleScript)
  • On Win: do <script> as "VBScript" (or to be specific, according to the dictionary: On Windows systems, the alternateLanguage is an "active scripting" language (such as VBScript) supported by the Windows Scripting Host. Not sure what "active scripting languages" are available...
I'm not aware of integrations with other languages without using the console as a 'man in the middle' but may be wrong...

liveme
Posts: 240
Joined: Thu Aug 27, 2015 5:22 pm
Location: down under

Re: Livecode and Python?

Post by liveme » Fri Aug 25, 2023 8:03 pm

Thanks Stam for sharing this knowledge about Mac/Win possible use of the "do as alternateLanguage"
and no, I didnt know about still... :P
* I will look for what "alternateLanguage" command has available for the Linux/Ubuntu and what language it would target.
I m keeping open to some other commands for other languages beside...
Thanks a lot anyway ! 8)

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Livecode and Python?

Post by stam » Fri Aug 25, 2023 8:07 pm

liveme wrote:
Fri Aug 25, 2023 8:03 pm
* I will look for what "alternateLanguage" command has available for the Linux/Ubuntu and what language it would target.
sadly I think this is Mac/Win/Web only:
Dictionary entry on Do as alternanteLanguage wrote:On other platforms, the alternateLanguage is not supported, and the do as alternateLanguage command will set the result to "alternate language not found"

liveme
Posts: 240
Joined: Thu Aug 27, 2015 5:22 pm
Location: down under

Re: Livecode and Python?

Post by liveme » Fri Aug 25, 2023 8:19 pm

Aoutch ! :shock:
:D

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Livecode and Python?

Post by mwieder » Mon Sep 11, 2023 4:22 pm

Great, I've just tested it in a terminal and its super cool that it can work in this way !! :D

I'm wondering, what are some method names in order to pass some values to a python or julia or some other language
without opening a console ahead ?
If you've got this working in a terminal window on linux and are just looking to do the same with the shell() function without having LC open a terminal window then you want the hideConsoleWindows command. The LC dictionary is wrong - ignore its note about the fact that the hideConsoleWindows command only works on Windows.

Code: Select all

   set the hideConsoleWindows to true

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”