Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve Django for an Electron app

I'm trying to create an Electron desktop app which has a Django application at its backend. There are several tutorials and blogs which mention how this can be achieved. I've tried those and it seems to be working, however there are some issues.

One of them for me is how to server Django in this case? For me the current way of doing it creates some unwanted delay making the app slow to start...

Generally, what needs to be done to create an Django/Electron app is to package (I'm using pyInstaller)the Django app into an stand-alone executable and then bundle that into an Electron app. The question is which server should be used for this case to server Django before packaging it with pyInstaller? At the moment I'm using cherryPy as a WSGI web server to serve Django.

However - is there a better alternative knowing that this will be used in an Electron desktop app? Maybe something faster, or more suitable for this task? What is the typical way of handling Django in this case?

like image 967
user1544500 Avatar asked May 30 '17 20:05

user1544500


People also ask

Can we use Django with Electron?

Generally, what needs to be done to create an Django/Electron app is to package (I'm using pyInstaller)the Django app into an stand-alone executable and then bundle that into an Electron app.

Can I use Django for desktop apps?

Django is a web framework, you can't use it to make desktop apps (except if you count those Electron and similar abominations as desktop apps, but, even then, they don't use Django).


Video Answer


1 Answers

First of all, if you app slow to start you can create custom loading-animation template with node which you will serve until your server will be ready, in this case you BrowserWindow flag show should be setup to false, you will "show" your window with window.show() on your custom "server_ready" event. Overall logic of your app should fit in: 1) You start electron app and on load serve browser window with loading... animation, then you spawn child process in which your django app will run, here you have the "bridge" between your electron-node events and python logic, this done as follows:

let django=child_process.spawn('python', [__dirname+'/django_folder/start_server.py']); 

Now variable django is your handler for communication with you django app. You can communicate as follows:

            let result_name = '';

            django.stdout.on(  
                'data',
                (data:Buffer) => {  
                    result_name+=data.toString('utf8'); 
                }
            );       

            django.stdout.on(
                'end', 
                (reasone) => { 
                   resolve(result); 
                }   
            );   

            django.stderr.on( //error in python will be directed here
                'data',   
                (buf : Buffer) => {
                   reject(buf.toString('utf8'));
                } 
            );  

django.stdin.write(JSON.stringify('notify your django app what you need from it'));

In python:

args=sys.stdin.readlines()[0] #your message in json from electron
args=json.loads(args)['data'] 
print result #send data back to electron

If your app is not very complex you probably can run in on localhost, so then your python process will notify you through event that it is ready you can reload BrowserWindow with localhost url and start to coordinate interaction communicating with child process.

like image 151
Anatoly Strashkevich Avatar answered Oct 18 '22 21:10

Anatoly Strashkevich