-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A simple working example for embedding dash in flask under a path #214
Comments
I'm also very interested in this. I want to use a Flask app for things like authentication, and use Flask for setting up the base html. But then use Dash for setting up dashboard pages. Anyway figured out how to do this? |
Yes, looking for a similar example. |
dito |
looking for the same! |
Likewise-- this would be super helpful! |
Okay. So I tried this out and created a gist - The main part of the code is creating the Flask Server, creating routes and passing the server to the dash app. |
@lazyspark eg. This would leave the rest of your Flask app the same, i.e. route '/' could stay the same. And you would specify where the URL path of the dash app. I think that would be more useful in the example. |
I think I figured out the answer to the original question. Your able to use the native Flask route decorator, and pass back the results from index() - which is a Dash instance method that For example:
When you go to the URL:
This will render the HTML output as shown in the Dash source code for the index() method - it contains a header, body, css, scripts, etc... which basically runs your app.layout inside a script tag. At this point you can modify the MyDashApp to return any HTML as you desire. You can Jinja with templating. OR you can create your own customer Dash class and override the index() method and create whatever you want. For example:
|
Am I correct that this doesn't help if the goal is to use authentication through the Flask app? I think that the Dash app will still be accessible without the authentication on the |
(note i accidentally setup my github account with two different handles and emails. i'm both @jacohn16 and @moonlight16) Hi @oriolmirosa As far as authentication, can you clarify your issue? How are you dealing with authentication currently? Maybe this is a separate topic? |
Hi, @moonlight16, you are right that the authentication issue is only tangential to this. I asked a question with the code I'm trying to use (and doesn't work) in the community forum in case you want to take a look: https://community.plot.ly/t/login-with-google-sign-on-dash-app/10447 |
@oriolmirosa I'm also working on using Google OAuth for a Dash app. I have a basic working example of using Google Login to authenticate against a list of valid email addresses. It's a little janky, so if you find a better approach I'd love to hear it. |
@lucaschapin This is great! I have adapted your example for my purposes (instead of a list of email addresses, I need to give access to everyone in my organization) and I think it works. I haven't been able to test completely because of issues with my server, but hopefully I'll be able to confirm in a couple of days. By the way, I think that your approach (overwriting the index) is the way to go given how Dash works. I'll link to your work in the community discussion I referred to above. Thanks! |
I wanted to get in on this thread becasue there isn't much help out there. @jacohn16's solution loaded the page but not the dash. When I dashboards.py:
Sorry to jump in for troubleshooting help but I can't find much help out there. EDIT: When running on chromium without addblock (mentioned here), the error changes: |
Same issue as @jackwardell did you find a fix? |
hi guys, Ideally we would want all Dash stuff to be available as a blueprint that i can use as part of my flask app. It should support standard @app.route . Is there any chance this will be supported ? |
My vote for making Dash more flask eco system friendly. To be able to use blueprints and @route and other decorators, flask-login's authentication, ... |
Same here . We are trying to incorporate dash into a larger flask
application and it's been very hard.
Flask is extremely trivial to get started - I think Dash will be much more
usable if it integrates with flask, rather than tries to replace it.
…On Wed, 1 Aug, 2018, 00:42 Aso, ***@***.***> wrote:
My vote for making Dash more flask eco system friendly. To be able to use
blueprints and @route <https://github.com/route> and other decorators,
flask-login's authentication, ...
I still like to use flask and user injected variables (e.g. current_user)
in the html template possiblly through jinja syntax. I like to create shiny
bootstrap base templates, them customise it through jinja with injected
Dash core components.
I know the philosophy is different in that Dash creates html front-end,
but there should be a way to around that.
It has great plotting functionality but current html components only suit
tableau / dashboard like pages, not a complex and fully featured web app
(maybe that's why it's named Dash!).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#214 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAEsU9kqY7EJ9SZNQMYSXroCaKdnKDhDks5uMKwxgaJpZM4SaZTN>
.
|
I also would like to see Dash become more of a component / app that can be deployed on another service like Flask or Django. However -- according to the Dash docs -- you can apparently pass your own Flask app to Dash -- so, the complete opposite of what we're all saying:
Essentially, it just means what we already knew: that Dash uses Flask under the hood. But this specific bit of functionality reveals a bit more about how that's being done... |
Hi, I tried using dash-google-auth but I'm getting an error. I did all of the steps from the README.md file and replaced the variables for client ID and client secret with real values like this:
However, when I ran python app.py and opened "localhost:5000" in my browser I got the following error:
I put the following into the “Authorized redirect URIs” field: http://localhost:5000/login/google/authorized Any idea why I might be getting this error? Thanks. |
Got the same error...please help! |
@delaleva you can see from the error message that the client_id isn't being set properly. os.environ.get() attempts to locate an environment variable in your bash profile, so the value here should be a key rather than the client id itself. If you want to set the config values directly (fine for testing but never do this in production), your code would need to look like this:
|
I using @login_required with @route in my app 。But i don't know how do insert current_user to the dash |
@sandys This sounds interesting (Dash app all within a single function). Can you give a little working example of how you are doing this? Would be greatly appreciated. |
#377 and |
@lucaschapin I have been trying to recreate your example straight out of the box however I'm getting an Internal Server Error displayed on the webpage with the following error in the terminal:
Any ideas how I can get around this? |
I had been struggling with this, and might have found a solution by cobbling together a few things that I found on Stack Overflow and deep within other GitHub threads:
Let me know if anyone finds this useful! |
@berryt08 looks like this issue happens with flask 0.12.3. I've updated my requirements file to use flask 0.12.4 which should solve it. If you're still having issues, I'd also try using Python 2.7 as I haven't tested out 3.X yet. |
@lucaschapin great stuff, thanks a lot! I can confirm it all works fine on Python 3.6. |
@rmarren1 just saw this PR; excited for it! |
@numpynewb , thank you so much! However, could you please help me to solve the following problem. I have several .html templates and several .py files with the Dash dashboards Using your suggestion I finally reach the point when my .html templates are rendering as well as the layout of my Dash apps, but the layout only. Do you know by chance what am I missing to be able to see the Dash app (not the layout, but all the data inside) in the depicted path? I desperately hope for your help or a hint. Thanks! |
@praskovy Great! I am glad that you found use in it. If you set up a simple working example of your issue as a snippet in GitHub, or even if it fits in this thread (the length of my post was pushing it, I think...), then I can take a gander. I cannot guarantee that my looking will help, but I am happy to do so. |
@numpynewb , thank you, I appreciate your help a lot! Here is the main.py script, which runs the website (for this one I used your solution, but just cut the login part):
Here is my app1.py script: When I comment this line app1.py script:
Here is my app2.py (the same situation that with app1.py):
Here is my home.html page:
I've tried different solutions, but only yours works for me somehow, it allows to load both .html templates and Dash apps. When I'm using this solution for the main.py script, I'm able to return the working Dash apps, but not able to render the .html templates:
From what I've read, I didn't find any solution so far which would be closest to the final outcome than yours. I would be vure thankful for any hint or advice. |
Hey @numpynewb , maybe if it would be interesting for you, I found a solution :) @Volodymyrk suggested this structure: #70 and it perfectly worked for my case - allowed me to render the .html template and return my Dash apps. Thank you, @Volodymyrk! server.py app1.py app2.py run.py Thanks a lot! |
Just a heads up that I'm keen to finally get something into the docs about this and have created an issue over at plotly/dash-docs#246. If you have any thoughts to add about the strategies I've suggested there, or any additional strategies, please chime in :) |
Were you able to use flask-login with this solution? |
Is there also a possibility to implement a "roles_required" functionality this way? I tried playing a bit with the protect_views function and changing login_required into roles_required, but I can't get it working. I would ideally only show certain pages to the administrator. With plain flask I would do:
But this only seems to work for html pages that I render within the app (as expected). Anyone? |
Thanks for your reply @numpynewb . However, I don't see how this works, Wrapping the function _add_url as follows doesn't do anything as well:
Unfortunately I don't understand much of the whole view_funct and the inners of dash, which makes this difficult. Any help is much appreciated! If this question is better asked on the plotly forum, let me know and I will ask it there. Thanks! |
You can find a fully working solution at: https://github.com/okomarov/dash_on_flask The flask app uses:
If you are NOT using the application factory, then the earlier answer is probably better suited. |
@Motta23 Importing layouts seems working fine, and there are multiple ways to redirect html. So far, I haven't been able redirecting dash callbacks in a multi app ( nested ) situation. A dash app may be located in a sub folder, and look like this:
Then, I modified your initial run.py:
|
The Dash guide now has a Chapter on integrating Dash with existing web apps, which includes a simple recipe for embedding Dash under a path. https://dash.plot.ly/integrating-dash Since this issue has now been addressed, please jump on over the the Dash community Forums for further discussion around this topic: https://community.plot.ly/c/dash |
me too |
@numpynewb `# somewhere to login
|
I currently put a empty page in dummy path and return stuff with callback after authentication. |
from flask import Flask
import dash
import dash_html_components as html
from dash.dependencies import Input, Output
class DashApp:
def __init__(self, flask_server):
self.app = dash.Dash(name=self.__class__.__name__,
routes_pathname_prefix='/dash/',
server=flask_server)
def setup(self):
self.setup_layout()
self.setup_callbacks()
def setup_layout(self):
self.app.layout = html.Button(id='some_id')
def setup_callbacks(self):
@self.app.callback(
Output('some_id', 'children'),
Input('some_id', 'n_clicks'))
def update_button_text(n_clicks):
return f"Hello Dash {n_clicks} clicks"
app = Flask(__name__)
dash_app = DashApp(app)
dash_app.setup()
@app.route('/')
def hello_world():
return 'Hello Flask World!'
if __name__ == '__main__':
app.run(debug=True) Consider using Mixins and splitting your code logically across files when the code gets large. |
So, for authentication you can block the view where dash was initially declared. In your case (/dummypath'). You need to create a flask log in manager, and there is a way to block the view . Try looking for creating protected view for dash apps using login manager |
Dash documentation lacks a simple but full working example of how to embed dash layout as a path in a flask app, so that the dash layout becomes a div in the flask and follows the base template. For example path '/plot&type=plot1' return dash layout for plot type 1.
I know that one can get the underlying flask app in dash, but how to get rendered dash layout in flask is not well documented.
The text was updated successfully, but these errors were encountered: