Developing applications for Apple Watch (iPhone and iPad) using Wolfram Language (Mathematica)
Translation of Stephen Wolfram's post, Instant Apps for the Apple Watch with the Wolfram Language .
I express my deep gratitude to Kirill Guzenko for help with the translation.
My goal is to take programming to the next level with Wolfram Language . And last year (see the article on Habrahabr " Stephen Wolfram: The Frontiers of Computational Thinking (report from the SXSW festival) ") we expanded the methods of using and deploying the language - on a working computer, in the cloud, mobile and embedded platforms, etc. A what about wearable gadgets? And in particular about the Apple Watch? A few days ago, I decided to see what can be done here. So I freed up my day for this thing and started writing code.
The idea was to write code using the Wolfram Programming Cloudbut instead of creating a web application or web API, I needed to get an app for Apple Watch. And, which is quite convenient - the first, preliminary, version of our Wolfram Cloud app is now available in the App Store:
It allows you to download applications from Wolfram Cloud directly to iPhone, iPad and Apple Watch.
In a way, it was adventure programming. The Apple Watch was just coming out, and the Wolfram Cloud app was just a preliminary version. However, for the past almost 30 years I have been creating the Wolfram Language advanced development environment. And I'm glad to note the fact that you do not need a lot of time to start receiving interesting Wolfram Language applications that run on the Apple Watch. And in less than a day of work - with the help of a small team - I developed 25 applications compatible with the watch:
I created all these applications by going through several steps - first programming them in the Wolfram Programming Cloud (on the website or in the desktop version), then deploying them in the Wolfram Cloud and connecting to the Apple Watch through the Wolfram Cloud app. And, although the applications were designed for the Apple Watch, you can, in fact, also use them through a browser or on your phone. There are links to web versions of applications that are presented in this post. To get these applications on your phone or watch, just go to this page and follow the instructions.
This page also contains all the source code for these applications in Wolfram Language, and you can use any system for working with Wolfram Language - Wolfram Programming Cloud (including free), Mathematica and so on - to experiment with the code yourself and possibly post your version of any application.
My first Apple Watch app
So how does it all work? For the first time working with the Apple Watch, I decided to write a simple test application that produces one random number. The main Wolfram Language code for this task is very simple:
Let the numbers on the watch be large and bold, and in general, with a random color:
We can immediately upload all this to the cloud with:
And, if you follow this link to the web browser, you will see a web application that creates a web page with a random number. ( Delayed in the code delays calculations until no one has entered the page or until it has been updated, so you will receive a new random number each time.)
How to send all this to the Apple Watch? First of all, all this should get on the iPhone first. And it is easy. Because everything that you uploaded to the Wolfram Cloud is fully accessible on the iPhone through the Wolfram Cloud app. To make this application easy to find, it’s worth adding an icon and a name. And if in the end it all works on the clock, then you should place it on a black background:
And now, if you follow this link through a browser, you will see an open version of the application there. Inside the Wolfram Cloud app on the iPhone, the application appears in the WatchApps folder:
Now, if you click on the application icon, you will run the Wolfram Language code in the Wolfram Cloud and again a random number will be displayed on the phone:
If you want to start the application again and get a new random number, just pull down at the top of the screen.
To get the application on the watch, return to the list of applications and click on the clock icon at the top and select the application. This will display it on the watch in the list of those applications that are associated with the phone:
Now just click on the RandomNumber app. In this case, the Wolfram Language code will be launched in the Wolfram Cloud, and after that a random number will be displayed on the clock:
With Wolfram Language it is very simple to create all sorts of "random" applications. Here is the key part of the Coin Flip app coin flip app :
That's all it takes to deploy the app on the Internet, on mobile devices and watches:
Someone might say that it is not practical to use all this complex set of technologies for such tasks. In the end, it's easy to just drop a regular coin. But this assumes that you have it somewhere (I, for example, do not have them). In addition, the Coin Flip app will produce a better pseudo-random sequence.
How about playing rock-paper-scissors with your watch? The main code for this task is again quite simple:
There isa huge amount of knowledge built into the Wolfram Language - and somewhere in the far corner there is knowledge that makes it very easy to create an application with the output of random Pokemon Random Pokemon app :
Here's how it works on the clock:
Let's now try something with more complex Wolfram code Language Here, for example, is the inventor of the words Word Inventor app , which creates a word from random vowels and consonants, alternating them (the resulting words often sound like the names of Pokémon or some kind of technical startups).
Clock that shows time
For lack of other things, people will presumably want to use the watch for its intended purpose. And since we are in the modern world with the Internet - even a watch can not do without a couple of cats. Below is the Kitty Clock code for Wolfram Language:
But on the watch itself it looks like this:
With this watch you can get crazy things. Based on our recent (see article on Habrahabr " 3/14/15 9:26:53 Celebrating the" Day of the Pi "century, as well as a story on how to get your very personal piece of pi ") and the very popular site My Pi Day , created in honor of Pi Day , below is a slightly more complex code for Pi watches, in which the current time is displayed in the context of finding these numbers in the Pi number:
Or add some more information:
Where are you at?
You can enable GPS tracking of your current location both on the Apple Watch and on your phone in order to find out your location. This makes it very easy to create Lat-Long app geolocation applications that show the width and longitude of your location on the watch (this example shows the coordinates of our headquarters):
I'm not quite sure that the program below can be useful (prove your location via Skype?). Actually, here is the Here & Now QR app , which shows your current coordinates and time in the form of a QR code:
Wolfram Language knows about many things, and one of them is geography . Here is the code that allows you to find the 10 volcanoes closest to you:
If you add a little more code, they will be shown on the map, and then this whole thing can be collected in the Nearest Volcanoes app :
Here is the code of the 3D Topography app , which shows a three-dimensional scaled map within a radius of 10 miles relative to your location:
Data arriving at the device
Once we made contact with Wolfram Cloud, we were able to use the real-time data contained in the Wolfram Knowledgebase. Among other things, there is data on the current position ( x , y , z , t ) of the International Space Station :
Given my location and using a bit of graphics in Wolfram Language, you can create an ISS Locator app :
As another example of using real-time data , you can give the Apple Quanting app code , which gives the stock price of Apple:
Below is the code for the Market Word Cloud app, which shows the symbol of the company’s shares, the size of which is set according to changes in the price of the previous day (today Apple’s stock price has grown and Google’s stock price has fallen):
Below is the full currency conversion code based on your current location Currency Converter app / a>:
With Wolfram Language, it’s not at all difficult to create a wide variety of applications. Here is the code for the Sunrise / Sunset app , which displays information about sunset and dawn wherever you are:
Setting up a more convenient display for the clock requires a little more code:
Wolfram Language contains real-time weather data :
Which we can display in in the form of beautiful built-in icons:
Here is the temperature data for the last week:
And if we add a little code, we can present it as an application with a graph of the temperature change. Temperature History app :
Sometimes the easiest way to get the result in Wolfram Language is to simply contact Wolfram | Alpha . Here's what Wolfram | Alpha will show if you ask the system about the time it takes to get a good tan (it determines your current location): And so, we have a real-time application, the Sunburn Time app , in which Wolfram | Alpha is called from using the design in Wolfram Language (the application gives the different time it takes to get a good tan depending on the type of skin).
Reports and data loading
Wolfram Language has access not only to data streams from its sources, but also to private ones; in particular - to the data from Wolfram Data Drop (see the article on Habrahabr " Wolfram Data Drop - the new Wolfram Research service ")
As a lover of collecting and analyzing personal data , I made a databin in Wolfram Data Drop, which tells me the number of unprocessed and unread messages Email. I created a scheduled task that runs in the cloud and generates a report of my unread messages. Now let's create the SW Email Backlog app , which, on demand, will send this report and display it on the watch:
Yes, the number of unprocessed and unread posts has recently increased at least in part due to the fact that now some additional time is spent on working on this blog.
Now everywhere you can meet databins of Wolfram Data Drop and, of course, you can make your own . From any databin, you can immediately make a clock application with a panel for displaying information. As, for example, the Company Fridge app , which receives information from a small temperature sensor located in the refrigerator in our headquarters (the cycle is caused by the compressor, and the peaks correspond to the opening of the refrigerator):
Typically, a databin receives data from a single source or device. However, you can create a databin that receives data from an application that runs on several different devices at once.
As a simple example, let's create an application that simply shows where in the world this application was launched. Here is the complete code for the Data Droplets app, ready to be uploaded :
This application does two things. First, when it is running, it adds the location of the device on which the application is running to the central databin in Wolfram Data Drop. And secondly, it displays a world map with the labels of the last 20 places from which the application was launched:
Realizing the application
The main reason to run applications on your watch is to be able to receive information directly from your wrist. However, you can use these applications to work with some external systems, for example, through the API.
As one very simple example, below is the full code of an application ready for unloading, which sends the owner of the application a card with a radius of one mile from its position, and sends it when the application starts:
Applications for creating other applications
Until this time, all the applications that we talked about were created from fixed fragments of Wolfram Language code that were deployed to Apple Watch. However, the Wolfram Language is a symbolic language, so manipulating application code is as easy as any other data. This means that using Wolfram Language to create and deploy applications on the fly will be very simple.
Here is a simple example for you. Let's say we want to make a clock app that gives a countdown of days until our next birthday. It would be very inconvenient to enter the date of your birthday right on the watch. But instead, the application can be placed on the phone, where the date of birth is entered, and after that it can create another application for the clock in real time, in which the countdown is for your date of birth.
Below we will enter the date of birth in the standard “ smart field ” for Wolfram Language , which works with any date format:
And as soon as we click the Submit button, this application will launch the Wolfram Language code in the Wolfram Cloud, which will generate a new application with the date we specified earlier, and then this application will be deployed and launched already on our watch.
Below is the full code that required to create a Birthday Countdown app that generates new, customized apps.
And here is the result of the generated application for counting down to my birthday :
You can make a wide variety of applications like the ones presented above. Here is an example application where you need to make a list of different places, after which you will create an application that displays different times for the respective places:
You can also use application generation to put yourself in the application. Here is the code for deploying other applications, the You Clock app . It allows you to take a picture of yourself with your phone and create an application that uses this picture as a clock:
In fact, you can approach this process even more generally: you can create applications that generate other applications, and they, in turn, also create your own apps!
More than i expected
When I decided to use Wolfram Language to create applications on the Apple Watch, I did not yet know how this would happen. Will the entire application deployment cycle for watches run smoothly enough? Will those applications that are so easy to create using the Wolfram Language look good on the watch?
I'm glad to say that everything went much better than I expected. Apple Watch appeared recently, so there were some questions about the deployment of applications on them, but they were resolved very quickly. But it became clear that many good applications for Apple Watch can be created even with a small amount of Wolfram Language code (tweet-a-watch-app? See article on Habrahabr "Компания Wolfram Research открыла сервис Tweet-a-Program: интересных программ на языке Wolfram Language, длина которых не превышает 140 символов"). При этом, меня весьма впечатлил тот факт, что менее, чем за один день работы, мне удалось разработать 25 полноценных приложений.
Конечно, все это стало возможно именно благодаря всей совокупности технологий Wolfram Language, которые разрабатывались почти 30 лет. И очень приятно наблюдать, как хорошо работают разработанные нами средства автоматизации, которые позволили претворить в жизнь ещё одну интересную идею — приложения для часов.
It's great to program in the Wolfram Language and watch the results of what happened - as in the case of writing applications for the Apple Watch. But in the end, more important is how convenient it is for the implementation of many different goals and ideas. The code presented here is a good help to figure it out. There are many areas of activity. You can create many useful, and just funny applications. The important thing is that Wolfram Language makes it easy to create applications for the Apple Watch that it can turn into everyday routine - this is another place where Wolfram Language functionality can be brought.