Django Development on an iPad Pro

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 some of my non-software work.

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 to prove it.

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:

  1. Open the console window in Pythonista and type in the following: import requests as r; exec(r.get('https://bit.ly/get-stash').text)
  2. Restart Pythonista

Done.

Now for Django

With StaSh set up, getting django running is very simple:

  1. In the Pythonista file browser, locate and select StaSh by navigating as follows: Script Library -> This iPad -> launchstash.py
  2. Once you select the file, hit the run button in the upper right corner, this will trigger the StaSh shell in the console window.
  3. 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.

  1. Download and install Working Copy (free for many use cases)
  2. Add your GitHub credentials in Working Copy
  3. Add your target repository in Working Copy
  4. Open Pythonista
  5. Select Open…
  6. In Locations choose Working Copy
  7. Hit select in the upper right corner
  8. Select your target repository
  9. 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:

import sys
sys.path

You’ll see a line in the response that looks like this:

/private/var/mobile/.../<your-django-project-name>

Copy that path. In your manage.py file, under the “import sys” line add the following:

sys.path.append("<copied path>")

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: http://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

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.

SSH Terminal

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

If you’re looking for a general purpose code editor, I’ve read positive things about GoCoEdit and Textastic, but have yet to try them out. I believe GoCoEdit pairs well with Working Copy.

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.

Documentation

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.

iPad Accessories

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.

Resources

To close out this post, here are the sites I found very helpful in getting my environment set up:

3 thoughts on “Django Development on an iPad Pro

  1. I appreciate the work you’ve done here, but I’m unable to reproduce the results you have from just following along.

    Just a bit of clarification if you don’t mind —

    The manage.py script you have mention loading, is that a new file I generate in Pythonista or is that supposed to already be in the Git repo somehow?

    Additionally, when passing sys.path into the console, the returned location is quite different than the one you posted. I can however get a path that looks just like that by passing in os.getcwd() from the script I’m in. Will that also work if I append that value using sys.path.append?

    And final piece, nothing at all happens when I attempt to run the script using the arguments you’ve provided. No errors, nothing. I’m noticing that your script has a lot more information than anything you addressed in this tutorial. Am I supposed to have the additional code that’s visible in your screenshot in my script?

    1. In this walkthrough, I assumed a pre-existing django project was in place on github. Manage.py is a function that comes along with a django project when you create it. So, if haven’t created a new project yet, you’ll need to do that before walking through this how-to. I’m not quite sure how relevant the other questions are until you have a running django project. Maybe give that a shot first…

Leave a Reply

Your email address will not be published. Required fields are marked *