Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Get the request header in Plotly Dash running in gunicorn

This is related to this post but the solution does not work.

I have SSO auth passing in a request header with a username. In a Flask app I can get the username back using flask.request.headers['username']. In Dash I get a server error. Here is the Dash app - it is using gunicorn.

import dash
from dash import html
import plotly.graph_objects as go
from dash import dcc

from dash.dependencies import Input, Output
import flask
from flask import request

server = flask.Flask(__name__) # define flask app.server

app = dash.Dash(__name__, serve_locally=False, server=server)

username = request.headers['username']
greeting = "Hello " + username

app.layout = html.Div(children=[
    html.H1(children=greeting),

    html.Div(children='''
            Dash: A web application framework for Python.
        '''),

    dcc.Graph(
            id='example-graph',
            figure={
                'data': [
                    {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                    {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
                ],
                'layout': {
                    'title': 'Dash Data Visualization'
                }
            }
        )
])

if __name__ == '__main__':
    app.run_server()


Any help would be much appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can only access the request object from within a request context. In Dash terminology that means from within a callback. Here is a small example,

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

app = Dash(__name__)
app.layout = html.Div(children=[
    html.Div(id="greeting"),
    html.Div(id="dummy")  # dummy element to trigger callback on page load
])


@app.callback(Output("greeting", "children"), Input("dummy", "children"))
def say_hello(_):
    host = request.headers['host']  # host should always be there
    return f"Hello from {host}!"


if __name__ == '__main__':
    app.run_server()
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda