This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

pwtan14262
pwtan14262

Hi,

Would like to know if October CMS support running of python script at the backend, if yes how can we do it. thanks in advance.

Mohsin
Mohsin

To run a python script you basically need to call the python binary file passing a python script as argument. So you can write a console command to achieve this!

pwtan14262
pwtan14262

Hi Moshin,

Many thanks for the guidance! i have created the command class, but how do i call and execute the class. Thanks again.

Tempestronics-Android
Tempestronics-Android

First register the command with Artisan using Artisan::add(new Acme\Blog\Console\MyCommand); then to execute it use \Artisan::call('acme.mycommand’);

pwtan14262
pwtan14262

Hi Tempestronics-Android,

Which file do i add Artisan::add(new Acme\Blog\Console\MyCommand); in? Can i use \Artisan::call('acme.mycommand’); in my model file in afterSave() function? Is it possible? Thanks in advance.

Mohsin
Mohsin

It’s a good idea to put Artisan::add(new Acme\Blog\Console\MyCommand); in the plugin boot() method since you defined the command in that plugin. You can call \Artisan::call('acme.mycommand’); wherever you like based on your logic incl. afterSave. That is possible! Here take a look at this plugin. It uses the Artisan::call in onClear which is a handler defined in a reportwidget so that it gets called from the OctoberCMS dashboard. So it’s entirely upto you where you call it! Like for example, if I want to enable users to upload python scripts and then execute it then I would go about putting that in a controller as onExecute and put a button called Execute to run the script.

Last updated

pwtan14262
pwtan14262

Hi Mohsin & Tempestronics-Android,

I did manage to get it to call the function fire() in MyCommand.php as shown below. But the command 'python test.py' doesn't seems to execute. It is suppose to generate a .txt file which is no being generated. Am i missing something here? Would appreciate all your help.

public function fire() { $this->output->writeln('python test.py'); }

The following code is my python script which reside in my Model folder.

!/usr/bin/python

author = 'pwtan'

import sys

def write(): print('Creating new text file')

try:
    file = open('try.txt','w')   # Trying to create a new file or open one
    file.close()

except:
    print('Something went wrong! Can\'t tell what?')
    sys.exit(0) # quit Python

write()

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.