Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Created May 2, 2012 18:28
Show Gist options
  • Save LeZuse/2578981 to your computer and use it in GitHub Desktop.
Save LeZuse/2578981 to your computer and use it in GitHub Desktop.
Flask browser detection
browser = request.user_agent.browser
version = request.user_agent.version and int(request.user_agent.version.split('.')[0])
platform = request.user_agent.platform
uas = request.user_agent.string
if browser and version:
if (browser == 'msie' and version < 9) \
or (browser == 'firefox' and version < 4) \
or (platform == 'android' and browser == 'safari' and version < 534) \
or (platform == 'iphone' and browser == 'safari' and version < 7000) \
or ((platform == 'macos' or platform == 'windows') and browser == 'safari' and not re.search('Mobile', uas) and version < 534) \
or (re.search('iPad', uas) and browser == 'safari' and version < 7000) \
or (platform == 'windows' and re.search('Windows Phone OS', uas)) \
or (browser == 'opera') \
or (re.search('BlackBerry', uas)):
return render_template('unsupported.html')
@cruisemaniac
Copy link

Thanks a ton for this! 👍

@sedataym
Copy link

Thanks

@James-Parsons
Copy link

Cool do you mind if I use this in a site I am doing?

@LeZuse
Copy link
Author

LeZuse commented May 15, 2015

@james Sure, no prob.

@raghavmittal101
Copy link

thank you so much for this solution. But it is showing opera browser as chrome. I am running Opera 38.0 on Ubuntu.

@mw3i
Copy link

mw3i commented Mar 23, 2020

WOOT WOOT thank you sir!

@miotke
Copy link

miotke commented Nov 23, 2020

Thank you for this. Great solution for my use. :)

@ekkapongs
Copy link

Thank you too.

@huseyinhealth
Copy link

thank you so much for this solution. But it is showing opera browser as chrome. I am running Opera 38.0 on Ubuntu.

It's because Opera is based on Chromium.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment