How to tell if user is mobile or desktop in backend?

I have some functionality that will differ between mobile/desktop. More than just styling but actually the input into a callback should be different depending on the screen width. Anyone know of some clever ways to get this info into the back-end?

Hi! Same problem here. Have you managed to reach a solution?

Here is a function I made for this a few months ago that looks at the request headers when the user opens the page in a browser. The User-Agent string describes what kind of device they are browsing on. If android or iphone are in the string, they are probably on mobile. It requires request from flask.

from flask import request
def user_on_mobile() -> bool:

    user_agent = request.headers.get("User-Agent")
    user_agent = user_agent.lower()
    phones = ["android", "iphone"]

    if any(x in user_agent for x in phones):
        return True
    return False
1 Like

@adamschroeder is there a way to have such a functionality implemented in Dash itself? It would really be in the spirit of “mobile first” to have some way of figuring out the screen size (or at least the most important breakpoints - I think most GUI components use the same breakpoints as Bootstrap: Breakpoints · Bootstrap v5.0).

Just throwing this out here, I’m by far no expert in this area - maybe something like that already exists?

The issue with using the device is that someone could be on a desktop but the app is running in a small window.

You can find another solution here:

3 Likes