Open multiple web browser tabs?
This works for me.
import ui import webbrowser def show_url_google(sender): webbrowser.get('safari').open_new_tab('https://www.google.com') def show_url_python(sender): webbrowser.get('safari').open_new_tab('https://www.python.org') def show_url_apple(sender): webbrowser.get('safari').open_new_tab('https://www.apple.com') v = ui.View(frame=(0,0,600,400)) v.add_subview(ui.Button(title='google', frame=(50,200,100,100), action=show_url_google)) v.add_subview(ui.Button(title='python', frame=(300,200,100,100), action=show_url_python)) v.add_subview(ui.Button(title='apple', frame=(450,200,100,100), action=show_url_apple)) v.present('sheet')
Thank you. I will use your code; however, I am still left wondering why some version of the calls to webbrowser.get (or get_new or get_new_tab)
doesn't work. Why are the subviews necessary? That is, why doesn't this work:
JonB last edited by
have you tried @on_main_thread? That is one difference between a buttin callback and a regular command...
@jonb I have never used that but I will look into it. I just naively thought that a script with those three lines would work. My coding confidence is eroded when supposedly simple things don't work as expected :-)
My objective is to open 3 tabs in (any) browser. I want the script to open these tabs with 3 different URLs that are partly constructed with speech input. Everything works fine until I try to open the 3 tabs. Only one is opened. I don't want any buttons or anything more to click. I thought that those three lines (obviously with different URLs) would do the trick but apparently this is more complex than it seems.
zrzka last edited by zrzka
Here's explanation of what's going on here ...
This one internally calls
UIApplication.openURL_(), which causes this:
- first call - Pythonista goes to background, because Safari is launched,
- subsequent calls do nothing, because Pythonista (any app) is not allowed to call
openURL_()when the app itself is in the background.
But this works if you have an iPad and both apps (Pythonista, Safari) are open side by side. When you try it, all three URLs are opened in Safari. If you don't have them side by side, there's no way.
safari_open_urlsin the attached sample.
This one does use in app browser -> panel. But it's buggy / unfinished, because it reuses same tab for all subsequent calls. When I check the implementation, it just sends notification with
newarg (ignored), which should control stuff like new tab, ...
default_open_urlsin the attached sample. All three URLs are opened in 3 seconds interval, one by one, in the same tab.
webview_open_urlsin the attached sample. This one works and nicely opens all three URLs at once. The only con is missing controls, but you can easily add them.
#!python3 def webview_open_urls(urls): # Present custom ui.View as panel, works import ui for name, url in urls: webview = ui.WebView() webview.name = name webview.load_url(url) webview.present('panel') def default_open_urls(urls): # Use default (inapp) webbrowser, which is buggy, because it doesn't open new tab, just one tab is reused import webbrowser import time for name, url in urls: webbrowser.open_new_tab(url) time.sleep(3) def safari_open_urls(urls): # Use Safari webbrowser, which doesn't work, you can't call openURL from background import webbrowser for name, url in urls: webbrowser.get('safari').open_new_tab(url) if __name__ == '__main__': webview_open_urls([ ('OMZ', 'http://omz-software.com'), ('Editorial', 'http://omz-software.com/editorial/index.html'), ('Pythonista', 'http://omz-software.com/pythonista/index.html') ])
@zrzka The last construction is exactly what I need. Thank you. I guess that I had no way to know that simply calling webbrowser.open_new_tab multiple times would not work as documented.
@jonb The UI webview solution works but I am interested in understanding what you were suggesting. Is there a way to this without UI that uses either the inapp browser or an external browser by utilizing @on_main_thread? If so, can you elaborate or perhaps give me an example? I tried creating a function that simply called webview.open_new_tab and then "decorating" with @on_main_thread but that didn't help.
JonB last edited by
@JonB Did you try running my app? It opens the urls in new tabs if the urls are different. I guess that the explanation for "why it works" is that one has to bring the application to front before pressing the next button and it is not possible with "on_main_thread" .
Here is a stackflow answer for a related question.
If you have editorial, you can use the following script to open multiple urls in editorial's in-app browser.
import webbrowser, clipboard ''' Install this worflow in editorial before running this program. editorial://add-workflow?workflow-data-b64=eNrFk09rwkAQxb9KmXNa7DWXEkWKEFSiIqWUMCajLq67y2bWKCHfvbNSipeeWuppltn3Hr_ZPx1gxcqaBtL3DiqNjaxgbf1hq22bXfdeiUdauY1FX0MCDkNDQ9paT0UwRpkdpFvUDSVQhYbtcalYk6RctR6PxOQlteu_vGvFext4sbetmOc3kmtMn_wEMj6zF9xVkTd35SjIEXK058rQfVlmjowcyG8YIPqlsD2QKdDs6NrtBsnDcy_6iXGBQUj54qIf6xOaiuolnVmyOJYUXqKiRcUrw0rnFmuqvwE8nQj10Nu2IZ8FgUFWFWp9gZR9EIWVOSYG0oEE4mbm4myQPv_1XY3NvzzhjwSMtCQhzlUeg2blNJXB66ZUpqxu_pOq4qSwfJtPRrPp4jHL83Ixz6bTcfHkhKz_BEm5US8~ This workflow is modified version of the following workflow. http://www.editorial-workflows.com/workflow/5819340242812928/QIzaT7pEP9c ''' urls = [ 'https://www.google.com', 'https://www.apple.com', 'https://www.python.org' ] clipboard.set('\n'.join(urls)) webbrowser.open('editorial://?command=open_multiple_urls_in_clipboard')