How to log client IP addresses?

How is this done within a Dash app? I tried and failed to get it working using a combination of app.server.route('/') and flask.request.remote_addr. Thanks!

You can access the IP address a request originates from within the corresponding callback like so:

from flask import request

@app.callback(Output('target', 'children'), [Input('input', 'value')])
def get_ip(value):
    return html.Div(request.remote_addr)

Thanks for the quick reply @nedned. I knew my lack of flask experience had me dancing around the answer.

No worries :slight_smile:

I think the main trick is that you only have access to the request global in an actual request context, which for Dash is in a callback.

import socket
print (socket.gethostbyname(""))

More on…ip address

Sorry to dig this up but it’s basically the only hit when looking for the topic…

How does that solution work? What is input or the value, respectively? They’re never used. When is the callback triggered?

I couldn’t get it to work in a minimal setting that should just print the ip address.

accessing flask.request.remote_addr will work anywhere you’re inside a Flask Request Context. In Dash, this means you can access it anywhere you’re inside a callback function. And that’s it, nothing about the input or output values of the callback matter. It’s up to you what you want to trigger the callback and do with the IP address.

If you want to create a minimal proof of concept, just make something simple like clicking a button returning the IP address to a placeholder div.

1 Like

So it’s not possible to have this be part of the layout function already?
It has to be within a callback?

I’ll come back with an M(non)WE once I’m back at my machine.

EDIT: This somewhat does it:

from dash import Dash
from dash_bootstrap_components.themes import BOOTSTRAP

from dash import Dash, html, callback, Output, Input
from flask import request

    Output('IP', 'children'),
    Input('btn-1', 'n_clicks')
def ip(_: int) -> html.Div:
    req: dict = request.__dict__.copy()
    return html.Div(
            html.Div(f"{key}: {val}") for key, val in req.items()

def create_layout(app: Dash) -> html.Div:
    return html.Div(
        className="app-div",  # type: ignore
            html.Button('Button 1', id='btn-1'),

app = Dash(
app.title = "TEST"
app.layout = create_layout(app)

def main(app: Dash) -> None:, dev_tools_ui=True, dev_tools_hot_reload=True)

if (__name__ == "__main__"):

However, this requires an option to be present that triggers the callback - a button in this example.
Can this be done without such an option right at call time but not after?
Calling the function in the layout, i.e., html.Div(id="IP", children=ip(0)), breaks the request context (why even? we usually can call functions that generate a layout, can’t we?).

Can a callback have no input? somewhat deals with the same issue which makes it cleaner looking than my button solution:

from typing import Any

    Output('IP', 'children'),
    Input('none', 'children')
def ip(_: Any) -> html.Div:

and in the layout:

                style={'display': 'none'},

But it still should be able to get this done without a callback shouldn’t it?

Hello @gothicVI,

You can use something like a div as the input and output:

def pull_IP(n):
    ### code
    return dasn.no_update

This in theory should only run once in your app, or when this div is displayed. :slight_smile:

1 Like