How I work with PyBites exercises on my iPad

Posted on Sat 21 September 2019 in Python • 4 min read

Update 2019-09-25: With the new iPadOS 13.1 it works very well to do challenges directly on PyBites code challenges website with an external keyboard!! If you're using iPadOS 13.1+ take a look at my new workflow instead.

Update 2019-09-22: The workflow in this article assumes that you have an external keyboard to your iPad. The paste back to the exercise won't work otherwise.

I’ve been doing a lot of Python exercises on the PyBites code challenges platform the last half year or so. Due to some limitation or restriction in Safari on iPad, it's not possible to easily do the challenges directly on the website.

Since I often like to do the exercises when I am at a café, on a plane or wherever I am, and do not usually have my laptop with me, I came up with a nice workflow for these challenges.

Pythonista Intro image

The steps I use is (they are explained in more detail later in this article):

  1. Go to the https://codechalleng.es website and choose an exercise.
  2. Copy the code from the “YOUR CODE” tab.
  3. Create a folder in Pythonista with the name of the exercise and create a file with the name suggested in the exercise.
  4. Paste the code from step 2
  5. On the exercise page at https://codechalleng.es copy the code from the “TESTS” tab.
  6. Paste the code in a file with the name suggested for the test in the exercise.
  7. Do the exercise code
  8. Run the tests in Pythonista
  9. Paste the code in the exercise page on https://codechalleng.es
  10. Save and run the tests on the website.

Setting up the exercise on my iPad

To begin, I use the Pythonista app for all Python coding on my iPad. It works great for a lot of coding, and it comes with a lot of libraries built in. You can, but it’s not easy in my opinion, add more third party libraries. But the one’s in the package usually comes a long way. Besides the builtins in Python 3.6 has a lot of libraries built especially for Pythonista. E.g. location with access to the iOS geo-location services, photos with Photo Library Access on iOS.

Pythonista also integrates with iCloud Drive. So I have a folder there called bitesofpy with sub folders for each exercise.

To do an exercise in Pythonista: - I go to the exercise on https://codechalleng.es (e.g. Bite 104. Split and join) and copy the code from the YOUR CODE tab.

Pybites copy code image

  • Create a new file in the 104 sub folder and name it accordingly to the exercise (in the 104 exercise - pipe.py) and paste the code.
  • I copy the code from the TESTS tab and paste it into a file called test_pipe.py

For the tests to work in Pythonista I need to make sure to do an

import pytest

at the top of the file if it’s not already there. And at the end of the test file I need to add the following command for the tests to run:

pytest.main()

Pythonista code and test image

The tests will not run in Pythonista otherwise. I run the test file in Pythonista to see that the test code runs and that it fails as expected.

Pythonista test output image

Doing the exercise

Now I can code the exercise. Pythonista also have debugging with breakpoints and variable watching. When I think I got it right I run the test code again.

Pythonista Intro image

If it succeeds I copy paste the code into the codechalleng.es web page and run the tests there.

Pythonista Intro image

On my laptop I can open the bitesofpy folder in my IDE and code there as well. So if I start coding on the iPad, I can later continue on my laptop if I haven’t completed the exercise, or if for some reason I need more advanced debug functionality.

For example I haven’t figured out how to debug the test code in Pythonista if it’s possible at all.

This workflow has worked pretty well for me, and I have done most of my PyBite exercises (131 when writing this article) on my iPad.

There are a few cases when there has been problems. For example when the exercise wants to create a temp folder to put some data file in, there is a permission error to write to the temp folder. So in those cases I have changed the code to write to the same folder as the code file, and then change it back to the temp folder when running the tests on the PyBites web site.

Conclusion

Most PyBites exercises are possible to run in Pythonista on the iPad with the above workflow. And I like that I don't have to bring my laptop with me to do the exercises.

There is a lot you can do in Pythonista. Browse the documentation to see the Pythonista Modules, which are the ones included apart from the standard libraries in Python 3.6.1 (currently). For example it has NumPy, Matplotlib and Requests in the package.