Django To Exe – Distribute your django project as a portable Windows exe application

django2exe is a project that helps you distribute your django project(with one or many apps) as a portable Windows application.


Why This?

My Computer Engineering course this semester required me to submit a mini-project which demonstrated the use of Structured and Object Oriented Application Design.The course teacher demanded an .exe application(standalone with no Internet connectivity, Gorram it! ). Since I didn’t want to get into setting up Visual Studio and learning .NET, I looked for tools to deploy portable web apps as win32 apps.I came across

  • Electron – Uses node.js (Need to learn node.js)
  • nw.js – Again Uses node.js (Need to learn node.js)
  • phpDesktop – Uses Php with Mongoose server with Cef (Bam! Something I can work with)

So I wrote some code and after a while, as usual, my PHP code started to look shit.Another thing I am good at is writing Django apps.So I searched and found solutions like py2exe, some scripts but they don’t guarantee everything will work as expected.Besides python GUI libraries are not quite portable by themselves.Finally, I decided to take up this project for good.

What I exactly did :

  • Downloaded WinPython (32bit) – This is a awesome portable python suite with lots of packages like pip,pyqt,numpy,scipy,cython,etc lots of them in there.
  • Stripped WinPython of whatever I didn’t need and whatever that can be installed with pip later and kept pyqt,cython which are not easily installable by pip.
  • Installed Django and Cefpython3 via pip.
  • Modified Cefpython3’s pyqt integration example and wrote .bat scripts to start and kill django server and configure window parameters
  • And the end result was

django2exe demo screenshot


Documentation/Guide : 

Requirements

  • Windows 7/8/10
  • Your Django Project can be converted to a simple portable solution
  • Your Django Project must use SQLite database
  • Your Project Dependencies should be installable via pip

Setting Up

  • Run setup.bat to install django and cefpython3.
  • After successful installation you can delete setup.bat and setup_requirements.txt
  • The app folder is a sample django project which you can delete.
  • Paste your Django project folder where app folder is placed(it is recommended to compile all .py files to .pyc and delete all .py files, this will protect your source code.If you don’t django2exe will compile all .py to .pyc but won’t delete your .py files)
  • In the config folder paste the icon you want on the application window.
  • Edit the config.json to get it all running.
    • project_dir_name -> Name of your django project folder (not the path)
    • Rest all settings are self-explanatory.
  • Install all your pip requirements and migrations using the terminal which can be launched by executing py-dist\scripts\cmd.bat
  • When all done, try launching the your application with launch.bat

Contents

  • setup.bat and setup_requirements.txt
    • installs django and cefpython3 using pip.Delete these files in production distribution.
  • launchapp.bat
    • script to launch the application with a console to see log errors.Delete this file in production distribution.
  • launchapp.exe
    • launchapp.bat converted to exe with invisible console.Rename this to your application name.
  • app/
    • Just a sample Django project for structure reference.
  • config/
    • contains icon for application window.
    • config.json for settings for the application.
  • py-dist/
    • The py-dist folder has portable python with some extra packages like pip installed
    • There is also a run.py in py-dist folder which launches the web server and the windows application.It also takes care of killing processes on exit.
    • What run.py does
      • A application is made with pyqt.
      • A cef component is placed into the application using cefpython3.
      • A manage.pyc runserver process is spawned on port 5423 on localhost.
      • cef webkit is made to navigate to 127.0.0.1:5432
    • NOTE: If you want to navigate to some other url or bind server to some other port edit the run.py file located in py-dist folder.
  • py-dist/scripts/
    • env.bat
      • starts portable python environment
    • cmd.bat
      • starts portable python environment and starts cmd in the environment
    • python
      • portable python console

Downloads

MediaFire Link : django2exe-dist.7z

NOTE : You will need 7-zip to extract the downloaded file!

I could have included django(the irony) and cefpython3 inside the download package, but my upload speed is SHIT.Every KB I don’t upload is precious time saved.

Screen Shot 2015-10-23 at 01.39.36

That’s why you will need to run setup.bat and download django and cefpython3 yourselves. The download package is 38MB and extracts to take up 200MB.Final distribution after setup.bat would be 300MB+


Issues/Improvements/Suggestions:

There is still a lot of room for improvements like including first navigation url in config.json, launching another window using pyqt, etc

If you are facing any issues or need help or have any suggestions, put them in the comments below.

Credits


Cheers!

Dismiss iOS On Screen Keyboard after editing TextField – Code Snippet

By clicking outside the TextField

override func touchesBegan(touches: NSSet, withEvent event: UIEvent){
self.view.endEditing(true)
}

In IBAction of some view

@IBOutlet weak var mytextfield: UITextField!
@IBAction func doSomething(sender: AnyObject) {
mytextfield.resignFirstResponder()
...
}

Return or Done key on the On Screen Keyboard

class xyz : UIViewController,UITextFieldDelegate {
...
@IBOutlet weak var mytextfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
mytextfield.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}


func textFieldShouldReturn(mytextfield : UITextField!) -> Bool
{
self.view.endEditing(true)
return true
}
...
}

Notes:

1.textFieldShouldReturn should be camel case as shown.Changing case makes the code ineffective.

2.Extend class from UITextFieldDelegate if you are using the third method

Trivia:

1.IB stands for InterfaceBuilder

2.NS stands for NeXTSTEP