11

After using Grunt for a couple of projects I decided to give Gulp a try.

Most of the projects we work on are Python based, and the way we usually run them from the command line is: 'python manage.py runserver'

With Grunt, i found the grunt-bg-shell plugin, and was able to run my command like this:

// see: https://npmjs.org/package/grunt-bg-shell
bgShell: {
  _defaults: {
    bg: true
  },
  runDjango: {
    cmd: 'python <%= paths.manageScript %> runserver 0.0.0.0:<%= port %>'
    //cmd: 'python <%= paths.manageScript %> runserver'
  }
}
grunt.registerTask('serve', [
'bgShell:runDjango',
'watch'
]);

Unfortunately, so far i have been unable to find a similar plugin for Gulp. I've tried gulp-shell, gulp-run, gulp-exec, all to no avail. With most im able to print my string on the console, but i havent been able to run the actual command.

Any ideas ?

Pablo Rincon
  • 999
  • 10
  • 23

3 Answers3

8

I'm using gulp-shell to run a Flask server. I believe it should work the same way with Django:

var shell = require('gulp-shell');
gulp.task('flask', shell.task(['. env/bin/activate && python my_script.py']));

Did you use another syntax ... ?

vcarel
  • 1,787
  • 1
  • 16
  • 23
8

You can do this:

var process     = require('child_process');

gulp.task('flask', function(){
  var spawn = process.spawn;
  console.info('Starting flask server');
  var PIPE = {stdio: 'inherit'};
  spawn('python', ['manage.py','runserver'], PIPE);
});

This spawns a new child process running 'python manage.py runserver' and passes the output of flask to gulp output stream.

brijeshb42
  • 182
  • 3
  • 10
  • See also https://stackoverflow.com/a/33132843/470749 and https://stackoverflow.com/a/51716306/470749 which similarly recommend that a Gulp file could use `child_process` `spawn`. – Ryan Jun 30 '20 at 20:09
1

It's a old question, but a good alternative is django-gulp (https://pypi.python.org/pypi/django-gulp/2.0.0). It will run your gulp default task when you run the runserve command (gulpfile needs to be in the same directory of manage.py).