Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Open multiple web browser tabs?
-
I would like my script to be able to open multiple browser tabs. I tried using webbrowser.open, webbrowser.open_new and webbrowser.open_new_tab but each time I get only one tab. Is there way to do what I want and can someone give me a simple example?
-
Are you using beta? the app store webbrowser only has open(), as far as i can tell....
One approach is to simply use a ui.WebView(), and present to a sheet. You don't get a back button, but thats easy to add. If you want something full featured, @Sebastian wrote a very nice webbrowser (with address bar, back buttons, etc) that is completely ui based, and lives in a panel tab, and you can open multiple such tabs.
-
@JonB Thanks. I am using the Beta and I was just trying to use webbrowser.open_new and webbrowser_new_tab methods which are there and documented but do not seem to do anything different from what webbrowser.open() does. I thought that perhaps this was a limitation of the in-app browser so I tried the other (documented) feature of specifying a browser to be used. webbrowser._new(safari-http://URL) does open Safari but I still can't get 2 tabs to be started. Likewise for Chrome. I will look into the Pythonista built browsers.
-
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:webbrowser.get('safari').open_new_tab('https://www.google.com')
webbrowser.get('safari').open_new_tab('https://www.python.org')
webbrowser.get('safari').open_new_tab('https://www.apple.com') -
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.
-
Here's explanation of what's going on here ...
webbrowser.get('safari').open_new_tab()
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.
See
safari_open_urls
in the attached sample.webbrowser.open_new_tab
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
URL
,reveal
withoutnew
arg (ignored), which should control stuff like new tab, ...See
default_open_urls
in the attached sample. All three URLs are opened in 3 seconds interval, one by one, in the same tab.ui.View.present('panel')
See
webview_open_urls
in 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.
-
I thought you said @enceladus 's code worked, which if true, you should be able to just call one of those methods, wrapped with
on_main_thread
and achieve the same result. @zrzka seemed to clarify that this launches safari, not in a tab. -
@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.
https://stackoverflow.com/questions/10072633/objective-c-iphone-open-multiple-urls-in-safari -
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')