<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Erik Petrovski on Medium]]></title>
        <description><![CDATA[Stories by Erik Petrovski on Medium]]></description>
        <link>https://medium.com/@erikpetrovski?source=rss-b3317f0cf9d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*-JOs5TES98h3HALzA_7Dwg.jpeg</url>
            <title>Stories by Erik Petrovski on Medium</title>
            <link>https://medium.com/@erikpetrovski?source=rss-b3317f0cf9d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 11:59:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@erikpetrovski/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Add AD authentication to your Dash dashboard]]></title>
            <link>https://medium.com/data-science/a-simple-way-to-integrate-dash-with-ldap-4d5d059ee3a6?source=rss-b3317f0cf9d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d5d059ee3a6</guid>
            <category><![CDATA[login]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[dashboard]]></category>
            <dc:creator><![CDATA[Erik Petrovski]]></dc:creator>
            <pubDate>Thu, 12 Nov 2020 11:09:39 GMT</pubDate>
            <atom:updated>2020-11-13T14:00:41.560Z</atom:updated>
            <content:encoded><![CDATA[<h3>Add AD authentication to your Dash app</h3><h4>A very simple way to integrate Dash with LDAP using Flask</h4><figure><img alt="It’s a locked dashboard!" src="https://cdn-images-1.medium.com/max/1024/0*lR5dPuc5n8lfkTpK" /><figcaption>Photo by <a href="https://unsplash.com/@danedeaner?utm_source=medium&amp;utm_medium=referral">Dane Deaner</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In most organizations, login credentials are managed by a directory service like Active Directory. If you manage to integrate <a href="https://pypi.org/project/dash/">Dash</a> with your organization’s directory service then users can simply log in to your Dash app with their existing credentials. A common way of doing this is by way of <a href="https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol">LDAP</a> (an open protocol for speaking with directory services like Active Directory) and <a href="https://pypi.org/project/Flask/">Flask</a> (a lightweight web application framework). In this post, I’ll demonstrate how to integrate LDAP and FLASK with Dash in Python in — as far as I know — the simplest way possible.</p><h3>Prerequisites</h3><p>Before getting into any code, you need to have LDAP already up and running. This is a major step that’s out of the scope of this article which only focuses on the Python side of things. If you need AD integration however, I’m assuming that you work in a pretty large organization with its own IT-department and hopefully that department is (1) not you and (2) willing to help!</p><p>Assuming LDAP is up and running, go ahead and clone the example code from <a href="https://github.com/epetrovski/dash_ldap">GitHub</a> by running:</p><pre>git clone https://github.com/epetrovski/dash_ldap</pre><p>We also need a few libraries to build a Dash app and make it work with LDAP — four in total. If you already use <a href="https://pypi.org/project/pipenv/">Pipenv</a>, just run the following in a terminal inside the cloned dash_ldap directory to get a full working virtual environment with dependencies installed:</p><pre>pipenv sync</pre><p>If you don’t want to mess with Pipenv, just install the dependencies you need with:</p><pre>pip install dash flask flask-simpleldap gunicorn</pre><h3>Walkthrough of app.py</h3><p>All the code you need is in <a href="https://github.com/epetrovski/dash_ldap/blob/main/app.py">app.py</a>. Let’s go through it line by line.</p><p>The first trick is to make Dash run inside a Flask app that serves your Dash app to the outside world. This is done by passing a Flask object to the Dash app’s server argument at initialization like so:</p><pre>app = Dash(__name__, server=Flask(__name__))</pre><p>We then define a Dash app for purpose of example. This app is just a simple placeholder but don’t worry, when you build your own app with advanced callbacks etc., it won’t require any changes to the LDAP integration setup.</p><pre>app.layout = html.P(&#39;Hello world!&#39;)</pre><p>You’ll now need to configure Flask for LDAP by updating Flask’s configuration dictionary. The details below is just a placeholder as your credentials will depend on your company’s LDAP setup — again, ask IT!</p><pre>app.server.config.update({<br>   &#39;LDAP_BASE_DN&#39;: &#39;OU=users,dc=example,dc=org&#39;,<br>   &#39;LDAP_USERNAME&#39;: &#39;CN=user,OU=Users,DC=example,DC=org&#39;,<br>   &#39;LDAP_PASSWORD&#39;: os.getenv(&#39;LDAP_PASSWORD&#39;)})</pre><p>You may have noticed that I’ve taken the liberty and added the LDAP_PASSWORD with an os.getenv(). I’m assuming that you would never store a password in code but use something like an <a href="https://help.ubuntu.com/community/EnvironmentVariables">environment variable</a> instead.</p><p>Very importantly, now we need to protect all of Dash’s view functions that have been registered with Flask with an authentication requirement by wrapping them with LDAP(app.server).basic_auth_required():</p><pre>for view_func in app.server.view_functions:<br>   app.server.view_functions[view_func] = LDAP(app.server).basic_auth_required(app.server.view_functions[view_func])</pre><p>Finally, we may want a way to run the Flask app quickly for testing purposes. Usually we would use app.run() but since the app is served on a Flask server, we use app.run_server():</p><pre>if __name__ == &#39;__main__&#39;:<br>   app.run_server()</pre><h3>Test it out</h3><p>You can confirm that the AD integration is working by running:</p><pre>python3 app.py</pre><p>Now navigate to <a href="http://127.0.0.1:8000/">http://127.0.0.1:8000</a> and you should see a login prompt. Punch in your credentials and you should see the ‘Hello world!’ frontpage.</p><p>Now that you’re pleased with having everything running smoothly, please remember that a crucial part to making this all work is the <a href="https://flask-simpleldap.readthedocs.io/en/latest/">flask-simpleldap</a> package. It’s one of those great Python packages that just works and solves a very unglamorous but crucial IT need.</p><h3>Run in production</h3><p>If you’re running your Dash app in production, you should be running the app on a production grade http server like <a href="https://gunicorn.org/">gunicorn</a>. You already have this installed, so just run:</p><pre>gunicorn app:server</pre><p>This works because in app.py, I’ve defined:</p><pre>server = app.server</pre><p>There you go, a login protected Dash app that integrates with a directory service and is fit for production!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d5d059ee3a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/a-simple-way-to-integrate-dash-with-ldap-4d5d059ee3a6">Add AD authentication to your Dash dashboard</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>