5

a short question: Is there a way to get my browsers viewport size by Flask?

I'm designing a page for many devices, for example: normal PC, iphone etc...

Thank you, FFodWindow

1
  • Flask apps are run server-side. To get this kind of information, you need something running on the client, like Java Script. Commented May 30, 2016 at 14:15

3 Answers 3

2

An ugly workaround with JavaScript, it works nonetheless:

Assuming your original route looks like the following:

@app.route('/user/<user>')
def user(user):
    return 'Hello ' + user

The route after the workaround:

@app.route('/user/<user>', defaults={'width': None, 'height': None})
@app.route('/user/<user>/<width>/<height>')
def user(user, width=None, height=None):
    if not width or not height:
        return """
        <script>
        (() => window.location.href = window.location.href +
        ['', window.innerWidth, window.innerHeight].join('/'))()
        </script>
        """
    return 'Hello %s (%s, %s)' %(user, width, height)
Sign up to request clarification or add additional context in comments.

Comments

1

That's not really possible until you have a request from the user and explicitly provide that data in the request (or use the UserAgent to map certain devices to viewport sizes, but the user can change the UserAgent so this isn't necessarily reliable).

Flask solely runs on the server, and thus has no idea what the client is. For example, I can send a request from my terminal or from another server or from another Python app and your Flask server would still respond without knowledge of where I sent it from. If I really wanted to, I could set all to the same UserAgent so that there was no way of distinguishing the three different clients.

The simplest way is to make your content responsive on the client side using CSS media queries.

2 Comments

Thanks for the reply, but i use a bokeh plot and you need to set its width and height at the beginning...
@FFoDWindow Regardless of what you're using, you still can't get the width and height on the server.
0

Rushy already provided a correct answer, but when you want to use flask and bokeh (I am in the same position right now) responsive design does not help in all cases (I am using a bokeh-gridplot and don't want that if accessed from a mobile device).

I will append "/mobile" to the links from my home page to the plot page on mobile devices (by checking the screen width with css) and then make the plot layout depend on whether "/mobile" was appended to the link. Obviously this has some drawbacks (if someone on desktop sends a link to someone on mobile or the other way around), but as my page is login-protected, they will be redirected to the login page in most of the cases anyways (if they are not already logged-in) and need to go to the plot-page manually.

Just in cases this helps someone.

1 Comment

I too was using bokeh and was looking to get a responsive chart and found this answer here My solution looks like this which scaled nicely with different screen sizes: plot = figure(plot_width=800, plot_height=500, x_axis_type="datetime") plot.sizing_mode = "stretch_width"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.