Use Pyto to put the LIKE price on the iOS home screen (and use Siri voice trigger)

Makzan
·
·
IPFS
·
Last week we used requests and JSON API to get the current price of $LIKE and $OSMO from Coingecko. We can take this code further and run it on the iPhone. Even put it on the home screen display or use Siri voice query.

Last week we used requests and JSON API to get the current price of $LIKE and $OSMO from Coingecko . We can take this code further and run it on the iPhone. Even put it on the home screen display or use Siri voice query.

The final result is the result of running on the iPhone home screen Widget and Siri Shortcuts respectively:

To run Python code in iOS, I usually use Pyto for this application. Pyto supports putting code on the main screen widget or Shortcuts, so I recommend using Pyto. Pyto is divided into a lightweight version and a Pro version. The Pro version is $9.99, a one-time payment, which is purchased for its functions and convenience.

Below is the code we wrote earlier.

 import requests
url = "https://api.coingecko.com/api/v3/simple/price?ids=osmosis,likecoin&vs_currencies=usd"

res = requests.get(url)
data = res.json()

like_usd = data["likecoin"]["usd"]
osmo_usd = data["osmosis"]["usd"]

result = f"Current LIKE/USD is {like_usd}, OSMO/USD is {osmo_usd}."
print(result)

Gist: https://gist.github.com/makzan/0fff74313b6a3b6d5b192afb5984586d

Runs in Pyto for iOS

In Pyto, create a new code file, enter the file name, and paste the above code.

Create Archives in Pyto

Then press ▶️ Play in the upper right corner to run, you can get the expected effect.

Using Pyto to run our LIKE token price code

optimize code

But think about it, if we run it in the small widget of the main screen, the output of text sentences is not easy to read, and we don't know how long this code will run.

Run the effect on the main screen

So, on the basis of the original code, I used datetime.today().isoformat() to add the current date and time (by the way, only the first 16 characters are taken and the T character is replaced by a space, so that the string is in Slightly easier to read in the main screen.

 now = datetime.today().isoformat()[:16].replace("T", " ")

In addition, I also changed the text output to short newline output, where \n is the newline symbol.

 result = f"LIKE: {like_usd}\nOSMO: {osmo_usd}\n({now})"

A slightly modified full version.

 import requestsfrom datetime import datetime

url = "https://api.coingecko.com/api/v3/simple/price?ids=osmosis,likecoin&vs_currencies=usd"

res = requests.get(url)
data = res.json()

like_usd = data["likecoin"]["usd"]
osmo_usd = data["osmosis"]["usd"]
now = datetime.today().isoformat()[:16].replace("T", " ")

result = f"LIKE: {like_usd}\nOSMO: {osmo_usd}\n({now})"
print(result)

Gist: https://gist.github.com/makzan/4b4cf8fcc097147ddc22e99acef3c6f7

add to main screen

In the editing state of the main screen, select the upper left corner (+) and select the Pyto widget. You can choose fine, medium and large running codes according to your needs. I will demonstrate with a fine size.

After selecting, it will first write "No selected script" which code to run is not selected. Click on Widget and select the Python code file we just saved.

Note: If you want to modify, you can long-press this Widget on the main screen, and select Modify, you can then select the code file to run.

Add Pyto Widget to the main screen

Below are the results of the run, so you can keep track of them. This method works with different JSON API data sources, and can also be used to subscribe to different information, such as weather and other data .

Main screen operation results

Execute code with Siri

Another iOS integration feature of Pyto is integration into Apple's Shortcuts app .

Shortcuts is Apple's programming automation application in iOS (and the latest Mac), which can integrate different applications from top to bottom with a visual interface, and the name of each Shortcut process can be added to the process name by "feeding siri" to trigger.

Shortcuts make voice trigger code

For example, I made this Shortcut of "What is today's price", ran the specified code in Pyto, and then used "Get Script Output" to obtain the Print output of the code, and then used "Show notification" to display the text results and read them out by the way.

After that, in addition to being visible on the main screen, you can also use Siri voice to trigger code execution and listen to the voice results.

Gaying on the same scene: "Hey Siri, there is no parking space in the home-returning enterprise"

By the way, I have another Shortcut that asks Siri if there is a parking space in the house. You can talk about how to do it when you have time.

— Makzan, 2021-12-01.


My name is Makzan . In addition to my full-time job, I usually hold local competitions and world competitions, or teach on-the-job training in programming and website development. Now he is transforming the face-to-face training content into e-books, online teaching materials, etc. So far, he has written 7 books and 2 video teaching courses.

I occasionally launch #ProgrammingWednesday every Wednesday , introducing Python or different programming techniques, including automated office document processing, and web crawling.

If my article is valuable, please subscribe to sponsor me to continue to create and share.

Subscription Sponsorship: https://liker.land/thomasmak/civic

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!

logbook icon
Makzan我管理世界職業技能競賽之網站技術項目、舉辦本地設計與開發賽事、開課分享技術心得。一個用網頁來表達自己的作家。
  • Author
  • More

期望與放下

甚麼是世界職業技能競賽

【最後兩天】買《所謂「我不投資」,就是 all in 在法定貨幣》親身體驗擁有 NFT