Detect whether code is being run from mobile

Is it possible for code to detect whether the browser is on a phone or small display?

There is a table in our app which seen from mobile looks terrible. We would like to detect if mobile browser is active, and reorganize rows into cards that you can swipe down conveniently.

Is there a variable we can use in the code to know the environment the code is being executed on so we can tailor the UX to it?

Bump - interested in ideas here. This feels like a very common case, and haven’t seen conclusive resolution searching in the forums.

Is there a way for python code to be aware of what UX context is it being run in?

It would be ultra helpful to change layouts for mobile apps, or even yet, to create mobile-first streamlit mobile apps.