iPhone and iPad users, do you know an app called Pythonista3 that allows you to develop with Python on iOS devices? If you just want to run Python, I personally recommend Carnets --Jupyter. With Pythonista3, you can also develop functions that can be used from outside Abri, such as various shortcuts and widgets, and create games.
This time using such Pythonista 3 ** A widget that displays Qiita trends and opens the page in Safari when clicked ** I have created, so I would like to introduce it.
In the case of this widget, the contents of Launcher.py
under ʻExample / Widget` can be used almost as it is, so based on that,
・ Ability to get trend titles and URLs from the top page of Qiita ・ A function to temporarily save the acquired information ・ A function to access Qiita again and update the information if a certain amount of time has passed since the previous acquisition of information
Added.
The number of columns, the number of rows in the folded state, and the font size variables are summarized after the import statement, so you can adjust the layout to your liking. It will be cramped and hard to push, but if you set the number of lines to 5 or more, you can display all daily trends (30).
qiita_trends_widget.py
import re
import requests
import appex, ui
import os
from math import ceil, floor
import webbrowser
import pickle
import time
COLS = 1
ROWS = 3
fontsize = 12
def get_trend():
trends = []
text = requests.get( 'https://qiita.com/').text
titles = re.findall('title":".{10,100}?",',text)
urls = re.findall('uuid":".{10,50}?",',text)
for i in range(30):
trends.append({'title':'', 'url':''})
trends[i]['title'] = titles[i][18:-7]
trends[i]['url'] = 'https://qiita.com/items/'+urls[i][17:-7]
trends.append({'lastUpdate':time.time()})
with open("trends.pickle", "wb") as f:
pickle.dump(trends, f)
return trends
class LauncherView (ui.View):
def __init__(self, shortcuts, *args, **kwargs):
row_height = 110 / ROWS
super().__init__(self, frame=(0, 0, 300, ceil(len(shortcuts[:-1]) / COLS) * row_height), *args, **kwargs)
self.buttons = []
for s in shortcuts[:-1]:
btn = ui.Button(title=' ' + s['title'], name=s['url'], action=self.button_action, bg_color='#73c239', tint_color='#fff', corner_radius=7, font=('<System-Bold>',fontsize))
self.add_subview(btn)
self.buttons.append(btn)
def layout(self):
bw = (self.width - 10) / COLS
bh = floor(self.height / ROWS) if self.height <= 130 else floor(110 / ROWS)
for i, btn in enumerate(self.buttons):
btn.frame = ui.Rect(i%COLS * bw + 5, i//COLS * bh, bw, bh).inset(2, 2)
btn.alpha = 1 if btn.frame.max_y < self.height else 0
def button_action(self, sender):
webbrowser.open(sender.name)
def main():
widget_name = __file__ + str(os.stat(__file__).st_mtime)
v = appex.get_widget_view()
if v is None or v.name != widget_name:
try:
with open("trends.pickle", "rb") as f:
SHORTCUT = pickle.load(f)
except:
SHORTCUT = [{'lastUpdate':time.time() - 86400}]
with open("trends.pickle", "wb") as f:
pickle.dump(SHORTCUT, f)
SHORTCUTS = get_trend() if time.time() - SHORTCUT[-1]['lastUpdate'] > 1800 else SHORTCUT #1800 seconds from the last time(Half an hour)Update after the above
v = LauncherView(SHORTCUTS)
v.name = widget_name
appex.set_widget_view(v)
if __name__ == '__main__':
main()
** Pythonista3 is good ** (I hope more packages can be used ...)