Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to export the rendered image in python #210

Closed
chenxinfeng4 opened this issue Dec 22, 2023 · 6 comments
Closed

How to export the rendered image in python #210

chenxinfeng4 opened this issue Dec 22, 2023 · 6 comments

Comments

@chenxinfeng4
Copy link

I'm the creating a stimuli delivering system for animal behavior training, named ArControl. I use the state machine to design task and convert task to "State Machine Cat" smcat file for beautiful visulazition.

The smcat is great, but how can I directly get the image file? I want to embed this export feature in my application.

I'm good at C++ and python programming, but I not good at js and web. Is there any simple way to post my smcat file to https://state-machine-cat.js.org/ and get the image back? I'd would be better if the installation is as small as possible.

@sverweij
Copy link
Owner

Hi @chenxinfeng4 it's probably easiest to use the command line interface for that. It runs on node.js and can be installed with node.js's default package manager.

I have no experience with calling node programs from python or c++, but I guess the thing that enables calling executables from an os or system package should do the trick.

state-machine-cat.js.org doesn't run an API - it's only a 'static' website...

@chenxinfeng4
Copy link
Author

Is it possible to deploy all the dependence into one folder? I can just access the smcat.exe to get all the functions. I don't want consumer to manully install the nodejs or npm. It's easy to deploy python program or java program, I have no idea how to deploy nodejs.

@sverweij
Copy link
Owner

Hi @chenxinfeng4 I've heard of solutions that bundle node applications, including the node.js run time & libs into single executables, but I have no experience with them.

@chenxinfeng4
Copy link
Author

Sad, something went wrong when I packaged the cmcat.
$ npm install --global pkg
$ pkg -t node18 bin\smcat.mjs -d

pkg@5.8.1
[debug] Bytecode of C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin\smcat.mjs is added to queue.
[debug] Stat info of C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin\smcat.mjs is added to queue.
Warning Babel parse has failed: 'await' is only allowed within async functions and at the top levels of modules. (5:0)
[debug] Directory C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin is added to queue.
[debug] Stat info of C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin is added to queue.
[debug] Directory C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat is added to queue.
[debug] Stat info of C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat is added to queue.
[debug] Directory C:\Users\666\AppData\Roaming\npm\node_modules is added to queue.
[debug] Stat info of C:\Users\666\AppData\Roaming\npm\node_modules is added to queue.
[debug] Directory C:\Users\666\AppData\Roaming\npm is added to queue.
[debug] Stat info of C:\Users\666\AppData\Roaming\npm is added to queue.
[debug] Directory C:\Users\666\AppData\Roaming is added to queue.
[debug] Stat info of C:\Users\666\AppData\Roaming is added to queue.
[debug] Directory C:\Users\666\AppData is added to queue.
[debug] Stat info of C:\Users\666\AppData is added to queue.
[debug] Directory C:\Users\666 is added to queue.
[debug] Stat info of C:\Users\666 is added to queue.
[debug] Directory C:\Users is added to queue.
[debug] Stat info of C:\Users is added to queue.
[debug] Directory C:\ is added to queue.
[debug] Stat info of C:\ is added to queue.
[debug] Deleting record file : C:
[debug] Deleting record file : C:\Users
[debug] Deleting record file : C:\Users\666
[debug] Deleting record file : C:\Users\666\AppData
[debug] Deleting record file : C:\Users\666\AppData\Roaming
[debug] Deleting record file : C:\Users\666\AppData\Roaming\npm
[debug] Deleting record file : C:\Users\666\AppData\Roaming\npm\node_modules
[debug] The file was included as bytecode (no sources)
C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin\smcat.mjs
[debug] The file was included as bytecode (no sources)
C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin\smcat.mjs
[debug] files & folders deduped =
smcat.mjs
[debug] The directory files list was included (1 item)
C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin
[debug] The directory files list was included (1 item)
C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\bin
[debug] files & folders deduped =
bin
[debug] The directory files list was included (1 item)
C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat
[debug] The directory files list was included (1 item)
C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat
[debug] Targets:
[
{
"nodeRange": "node18",
"platform": "win",
"arch": "x64",
"output": "C:\Users\666\AppData\Roaming\npm\node_modules\state-machine-cat\smcat.exe",
"forceBuild": false,
"fabricator": {
"nodeRange": "node18",
"platform": "win",
"arch": "x64",
"binaryPath": "C:\Users\666\.pkg-cache\v3.4\fetched-v18.5.0-win-x64"
},
"binaryPath": "C:\Users\666\.pkg-cache\v3.4\fetched-v18.5.0-win-x64"
}
]
C:\snapshot\node_modules\state-machine-cat\bin\smcat.mjs:3
import executeCommandLine from "../dist/cli/execute-command-line.mjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at new Script (node:vm:102:7)
at Socket. ([eval]:18:19)
at Socket.emit (node:events:537:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)

Node.js v18.5.0

@sverweij
Copy link
Owner

Hi @chenxinfeng4 it seems that pkg doesn't support modern javascript. As state-machine-cat uses modern javascript, that probably means pkg is not suitable to package it.

I've searched a bit for alternatives for single executable applications on nodejs, but it turns out none of the ones I found will work.

  • nexe is one, but that hasn't seen maintenance in a while.
  • node.js has experimental support to generate single executable applications out of the box as well, however it's limited to old-fashioned javascript only at the moment (from https://nodejs.org/api/single-executable-applications.html: The single executable application feature currently only supports running a single embedded script using the CommonJS module system.)

Alternatively, if you want to run a modern node module from a single folder this should work:

  • install the module (e.g. npm i state-machine-cat) in that folder
  • copy the node executable to that folder (which, on windows, you apparently do like this: node -e "require('fs').copyFileSync(process.execPath, 'my-node.exe')" - source: node.js documentation )

All you need to run state-machine-cat should now be in that folder. To run it, in that folder:

my-node.exe node_modules/state-machine-cat/bin/smcat.mjs

Note

I have tried this on my own machine only, which is not a windows machine

hope this helps.

@chenxinfeng4
Copy link
Author

That seems a great solution, thanks for your kind help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants