I don’t write a lot of code. But I used to and I miss it. So I decided to kick off a side project to compliment my non-software work over at Evidence Based Athlete.
Incidentally, I also picked up the latest iPad Pro and thought it’d be a fun challenge to see if I could stand up a django environment on it for local development.
Luckily, many others have blazed a trail for django on iPad. Actually, it’s damn near a highway now. But, I found many of the write-ups incomplete for my needs. So, I figured I’d create my own post outlining how I got it all working. Oh, did I forget to mention I got it working 😉 Here’s a screenshot for proof:
Python on iPad
First, thing first. Python. I learned my choice of Django on the iPad was fortuitous. As of the publish date of this post, python is one of the few languages you can run natively on an iPad. This is thanks, in large part, to the incredible Pythonista app – a fully functioning python IDE.
Apparently, Pythonista began as a simple Python text editor and morphed into a hackable development environment including Python 2 & 3 support and bundled with popular packages like flask and numpy.
Step one, buy and install Pythonista for iPad. Easy, peasey.
Add Python Libraries with StaSh
Even though Pythonista comes standard with many useful python libraries, django isn’t one of them. Out of the box, Pythonista also doesn’t include a way to easily add libraries. That’s right, no pip 😢. But, as I mentioned, it is hackable, which is where the StaSh add-on enters the picture.
StaSh is a useful shell for pythonista that enables commands like pip. It’s probably great for a bunch of other stuff, but I don’t really care about any of that right now. I just want django. And if you’re reading this, you’re probably in the same boat. To install StaSh:
- Open the console window in Pythonista and type in the following:
import requests as r; exec(r.get('https://bit.ly/get-stash').text)
- Restart Pythonista
Now for Django
With StaSh set up, getting django running is very simple:
- In the Pythonista file browser, locate and select StaSh by navigating as follows: Script Library -> This iPad -> launchstash.py
- Once you select the file, hit the run button in the upper right corner, this will trigger the StaSh shell in the console window.
- In this shell, type in:
pip install django
Version Control (aka Getting Your Code into Pythonista)
Pythonista gives you access to a few locations within your iPad to store code, but going that route is a bit cumbersome, plus using some sort of version controlled repository is ideal for most applications. Luckily, another excellent app is here to save the day – Working Copy.
Taking a quick step back, my planned workflow includes:
- Using GitHub with Working Copy
- Local development on the iPad
- Eventual release deployed via some venue like Python Anywhere
Prior to pursuing this Django on iPad project, I’d already established a repository on GitHub for my code. So all I needed to do was download Working Copy, register my GitHub account and add my repository to the app.
This may feel like extra steps, but assuming your code will eventually migrate to some production server, or you’ll have multiple contributors or you’ll be developing on multiple machines – this really is the way to go. When I make changes within Pythonista, they are automatically registered in Working Copy where they can easily be committed and pushed up to GitHub.
- Download and install Working Copy (free for many use cases)
- Add your GitHub credentials in Working Copy
- Add your target repository in Working Copy
- Open Pythonista
- Select Open…
- In Locations choose Working Copy
- Hit select in the upper right corner
- Select your target repository
- Click open
Fire Up Your Django Project
Ok, it’s not quite that simple. We do have to deal with a couple of iPad specific limitations now. Pythonista only supports a single python thread and because of the way the app’s console is trigged with messages, it makes sense to minimize Django’s very talkative nature. To this end, we must add some flags for runserver. We also need to inform python of the location of our Django project.
Open manage.py in Pythonista and with that file open, activate the console window. In console type the following:
You’ll see a line in the response that looks like this:
Copy that path. In your manage.py file, under the “import sys” line add the following:
You may need to restart Pythonista at this point.
Finally, open manage.py again, and press and hold the run button in the upper right corner. This will open a dialog box that enables you to pass parameters.
Add the following text to that box:
runserver --noreload --nothreading -v 0 instead
If you’re getting a noreload error and something about valid port numbers (“noreload” is not a valid port number or address:port pair.”), it’s probably due to smart punctuation on the iPad.
This iOS feature is automagically converting your double “-” into a single, longer “-” which python doesn’t know what to do with. You need to go into the settings and turn off smart punctuation to get it working.
Settings->General->Keyboard->Smart Punctuation (toggle this off)
Go back and type in the text from above into the dialog box, it should work this time. If it does, you’ll see the following in the console window:
And now you can fire up safari and access your project at the url specified in the console window: https://127.0.0.1:8000
That’s it. You’re up and running. Develop to your heart’s content.
Additional Things To Explore
In the process of reading other articles and getting this all running on my iPad, I noticed certain recommendations come up time and time again. My needs are quite basic right now, but I’m sure I’ll find the need for more advanced features as time goes on. Below is a quick look at some of the additions I encountered in case they’re relevant for your needs.
Black Mamba is another add-on (like StaSh) for Pythonista that greatly enhances its function. As far as I can tell, it enables features like keyboard shortcuts and drag and drop, things you’d want if you’re going to spend considerable amounts of time developing within Pythonista. Nearly every write up I encountered on Pythonista praised Black Mamba.
I’ve read great things about the Blink and Terminus apps for ssh. Most are operating under the premise of using an iPad as a thin client paired with a server elsewhere to execute your code, e.g. Digital Ocean. I have no experience with this yet but may play with it in the future. Apparently, Blink (a paid app) supports the Mosh protocol, which is essentially UDP for an intermittent connection – this is important because without Mosh the iPad will basically close your connection after a short period of you being in another app (due to iPad multitasking limitations). Blink also offers sandboxed local terminal access. Sounds cool.
General Purpose Code Editor, i.e., an editor for more than just python
Coda is apparently a solid app for editing and developing HTML, CSS, etc. Again, no need for that right now, but I can see how it might be helpful while working on django templates.
Ok, I actually have this potentially useful little bugger installed. Dash is an app that allows you to download documentation for just about every coding-related subject matter you could care about. Most pertinent to the task at hand, it includes Django documentation. I haven’t played around with it much, but the whole point of getting django running locally on the iPad is to support offline development right? This fits nicely with that use case.
I didn’t feel like shelling out the big bucks for Apple’s smart keyboard folio and I already had the ultraportable Logitech keys to go bluetooth keyboard, so I picked up the highly rated and extremely cheap ESR Yippee case on Amazon. So far, I can’t complain.
I also got this very inexpensive Amazon Basics tablet stand for greater adjustability of viewing angle and for applications when I want to prop the iPad up in the portrait orientation. It works fairly well for this. I probably wouldn’t want to use it this way outdoors for fear of a strong wind sending my iPad crashing over though.
Finally, and not that they’re especially applicable to the use case outlined in this post, but rounding out my accessories are an apple pencil and the PaperLike screen protector. Even though these won’t help you with django development, I can’t recommend these two items highly enough.
To close out this post, here are the sites I found very helpful in getting my environment set up:
- Coding on an iPad Pro in 2019
- How to code with iPad (Pro). Make your 2019 year of mobile development.
- Peddals Blog: Install and Setup Django in Pythonista3 (iOS app)
- Python IDE on the iPad Pro
- Setting up an iPad for coding is my greatest feat as a computer user
- Using the iPad Pro as my development machine