I am trying to make a simple program that opens the web browser when you go to a specific URL in flask.
I am using nginx with uwsgi, to work with flask, running on ubuntu desktop 18.04.
from flask import Flask
import webbrowser
app = Flask(__name__)
#app.route("/test")
def test():
#this is where a new webbrowser should be opened:
webbrowser.open_new_tab("https://google.com")
return "test!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
I expect a new tab of the webbrowser to be opened on the server machine but nothing happens
Do you have a default browser set? You need a default set.
From webbrowser:
If the environment variable BROWSER exists, it is interpreted as the os.pathsep-separated list of browsers to try ahead of the platform defaults.
Another example from the same Python reference page demonstrates you need a window open to use open_new_tab() function:
Here are some simple examples:
url = 'http://docs.python.org/'
# Open URL in a new tab, if a browser window is already open.
webbrowser.open_new_tab(url)
# Open URL in new window, raising the window if possible.
webbrowser.open_new(url)
Ideally, You create a controller object specifying your browser of choice from the table in that link, such as "mozilla", "chrome", "safari" etc., then use the open_new_tab() function on that controller.
https://docs.python.org/3.5/library/webbrowser.html#browser-controller-objects
UPDATE:
So I tried this
import webbrowser
def main():
# this is where a new webbrowser should be opened:
webbrowser.open_new_tab("https://google.com")
return "test!"
if __name__ == "__main__":
main()
And I can open a new tab irrespective if a window is open.
So it works for a simple python script.
Are you saying that when you run you flask app, and try to do a GET request at http://yourip-or-domain-name/test your browser doesn't open?
(Assumption here is port 80 as you don't bind an explicit port in your app.run() call.
Related
I have a browser code built in python using PyQt5 and I want to implement a functionality which is whenever the browser receives an API call from a website to some other service it should open that in the default browser.
For example, whenever we want to login to a website using google and we click on the google option, we get a new window to select our google account. That's the same functionality I want to implement.
Find the complete code here: https://pastebin.com/41n9eghQ
class WebPage(QWebEnginePage):
linkClicked = Signal(QUrl)
def acceptNavigationRequest(self, url, navigation_type, isMainFrame):
if navigation_type == QWebEnginePage.NavigationTypeLinkClicked:
self.linkClicked.emit(url)
return False
return super(WebPage, self).acceptNavigationRequest(
url, navigation_type, isMainFrame)
The above class inside the code accepts and processes the in-browser navigation requests.
def createWindow(self, webwindowtype):
import webbrowser
try:
webbrowser.open(to_text_string(self.url().toString()))
except ValueError:
pass
The above function opens the window in default browser whenever an API call gets triggered, the only problem here is right now I am passing the url of the current website i.e. self.url() and not the url of the API and I don't understand how to do so.
Is there a way to capture the API request and pass that url to the webbrowser.open() function.
Any help would be appreciated, thanks for your attention!
This question already has answers here:
How can I open a website in my web browser using Python?
(10 answers)
Closed 1 year ago.
I want to start my Flask application directly in the browser when I run my Python file. I now just copied the localhost address into my browser and started it that way. Is it possible to do it without copying the link every time?
Try this:
import webbrowser
from flask import Flask
app = Flask(__name__)
#your staff
#app.route("/")
def hello():
return("Hello World!")
def open_browser():
webbrowser.open_new('http://127.0.0.1:5000/')
if __name__ == "__main__":
# the command you want
open_browser()
app.run(port=5000)
Use the webbrowser module:
import webbrowser
webbrowser.get("google-chrome").open("https://www.bing.com")
You can replace 'google-chrome' with another browser you would like.
I've written a script which opens Chromium and keeps it open for a set time, but I want to be able to set a proxy within the python script before opening Chromium. Is this possible? I've googled for some time, but I can't find how to do it.
Thanks in advance.
import os
from time import sleep
import webbrowser
import random
import time
timeDelay = random.randrange(57, 100)
def search():
#new browser object
chrome = webbrowser.get('chromium-browser')
#search engine startpoint
google = chrome.open_new("https://www.google.com")
if __name__ == "__main__":
sleep(0.5)
search()
time.sleep(timeDelay)
hey guys I been trying to host a local server on port 22222 and when someone open it on the browser I get a msgbox saying yes / no - if you click yes it will accept the connection and show index.html basically, if you click no it will return nothing or something, anyway it works perfectly BUT
I'm trying to trigger a second msgbox after you click on the first one with a delay(after command), when you access the first time and click yes, it serves the page but the Established msg does not show up, if you refresh the page than it retriggers the Established msg and than ask again if you want to accept the connection (Bug Alert lol)
basiclly you need to run this code open 127.0.0.1:22222, click yes, than you will not see the its_ok msgbox, unless you do all this again
import sys
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as Brain_Link
import tkinter
import tkinter.messagebox as mbox
window = tkinter.Tk()
window.wm_withdraw()
window.attributes("-topmost", True)
def Start_Brain_Link(*args):
Brain_Link(*args, port=22222)
def its_ok():
mbox.showinfo('A.I','Brain-Link Established!')
def its_bad():
mbox.showinfo('A.I','Brain-Link Attemp Blocked!')
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers (self):
if mbox.askquestion("Warning!", self.client_address[0]+" Requested Brain-Link!", icon='warning') == 'yes':
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
window.after(1000,its_ok)
else:
window.after(1000,its_bad)
if __name__ == '__main__':
Start_Brain_Link(CORSRequestHandler, HTTPServer)
I'm writing a Python Script using webbrowser module to automatically open the desired webpages.
The issue I'm facing is that I'm only able to open the webpages on different Browser windows and not on the same Browser window on different tabs.
Below is the code that I'm using.
#! /usr/bin/python -tt
import webbrowser
def main():
webbrowser.open('url1')
webbrowser.open('url2')
webbrowser.open('url3')
if __name__ == '__main__':
main()
I want to open all these links on the same web browser window on separate tabs and not on different browser windows.
Thanks :)
You need to use webbrowser.open_new_tab(url). For example...
import webbrowser
url = 'http://www.stackoverflow.com'
url2 = 'http://www.stackexchange.com'
def main():
webbrowser.open(url2) # To open new window
print('Opening Stack Exchange website!')
webbrowser.open_new_tab(url) # To open in new tab
print('Opening Stack Overflow website in a new tab!')
if __name__ == '__main__':
main()
In python 3.6, a complete answer will include both webbrowser.open_new() and webbrowser.open_new_tab() from the webbrowser docs.
import webbrowser
def main():
# print(webbrowser._browsers) # for Python 3.x to determine .get() arg
browser = webbrowser.get('firefox')
urls = ['url1', 'url2', 'url3']
first = True
for url in urls:
if first:
browser.open_new(url)
first = False
else:
browser.open_new_tab(url)
if __name__ == '__main__':
main()
Enjoy the code. +1 if it helped you out. Cheers!
Simply webbrowser.open_new_tab('url')