OctoPrint Python Terminal Code

Hi community,

I want to use functions of OctoPrint through Python terminal, but not from the GUI, does anyone have example Python code to run OctoPrint directly?

Thanks!

To call OctoPrint directly you must be in the same Python process as it, which means you cannot do it directly from the Python 'terminal'. The only way to get your code to directly access OctoPrint is through a plugin.

If you want to run your own Python scripts, you would have to go through the REST API:
REST API β€” OctoPrint documentation

Thanks Charlie. Using the API is definitely a good idea. I can send out a GCode command through API, but are there any way to receive the response? In the Octopi interface's terminal tab, a lot information is been updated, can I get that from API?

Due to a limitation of the printer's communication protocol, you can't wait and get a response to your command. The information in the terminal is sent to the UI using a SockJS websocket, and you just get the list of incoming commands every time it updates.

https://docs.octoprint.org/en/master/api/push.html