Skip to content
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

Allow serving Flask apps #1215

Merged
merged 2 commits into from
Apr 1, 2020
Merged

Allow serving Flask apps #1215

merged 2 commits into from
Apr 1, 2020

Conversation

philippjfr
Copy link
Member

Allows serving flask app using pn.serve, e.g.:

import panel as pn
from flask import Flask

flask_app = Flask(__name__)

@flask_app.route('/app')
def hello_world():
    return 'Hello, World!'

def panel_app():
    return "# This Panel app runs alongside flask, access the flask app at [here](./flask/app)"

pn.serve({'/flask': flask_app, 'panel': panel_app}, port=5001)

This will serve the panel app on localhost:5001/panel and the flask app on localhost:5001/flask/app.

@codecov
Copy link

codecov bot commented Apr 1, 2020

Codecov Report

Merging #1215 into master will decrease coverage by 0.13%.
The diff coverage is 25.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1215      +/-   ##
==========================================
- Coverage   87.61%   87.48%   -0.14%     
==========================================
  Files         106      106              
  Lines       12574    12601      +27     
==========================================
+ Hits        11017    11024       +7     
- Misses       1557     1577      +20     
Impacted Files Coverage Δ
panel/io/server.py 37.65% <25.00%> (-2.35%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1ef5792...14795a5. Read the comment docs.

@philippjfr philippjfr merged commit d79ac87 into master Apr 1, 2020
@philippjfr philippjfr deleted the serve_flask branch April 1, 2020 14:01
@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Apr 1, 2020

Wow. As a potential user of this I would like to learn what the best deployment strategi is for different use cases.

Panel on Bokeh server?
Panel on Django server?
Panel on Flask server?
Panel on Voila server?

And can I run Panel, Flask and the tranquilizer api on all those servers? How will they perform? What are the pros and cons?

This raises many questions. Can I use all the Flask functionality? For example can I build advanced rest apis using Flask extensions with open api documentation? Can I serve dynamic files using Flask? Would it be a good idea to mix flask and panel to a large extent? Could I embed Panel apps in the Flask apps I serve? And how? And whats the architecture. Is the flask server running. Or is the flask app running on Tornado?

Running on multiple servers as well as in notebook is a super power. But currently not documented and experiences not shared.

Request: Some kind of documentation and examples. :-)

@philippjfr
Copy link
Member Author

Yes, we have funded work to document deployment options, so it's definitely coming.

@jbednar
Copy link
Member

jbednar commented Apr 1, 2020

As one specific point among many that need documentation, if you used @flask_app.route('/') above, would the flask app be at localhost:5001/flask? If so, that seems like a more suitable first example to me...

@philippjfr
Copy link
Member Author

if you used @flask_app.route('/') above, would the flask app be at localhost:5001/flask? If so, that seems like a more suitable first example to me...

No that explicitly does not work, flask apps may only be served on a subpath.

@philippjfr
Copy link
Member Author

Well it was certainly beyond my ability to route things, might give that another go.

@jbednar
Copy link
Member

jbednar commented Apr 1, 2020

Ok, I now see that in the code "Flask apps must be served on a subpath.". Definitely needs explaining, then...

@pgierz
Copy link

pgierz commented Apr 2, 2020

This looks very very cool....looking forward to seeing some examples!

@nitrocalcite
Copy link

This is really cool! However, I also wanted to serve a Tornado app alongside panel; though I figured it was possible, I couldn't find any docs.

So, for future comers to this thread, you can pass extra Tornado RequestHandlers to the Bokeh server underlying Panel using the (undocumented) extra_patterns kwarg in pn.server or pane.show. You should set it to what you'd expect for a Tornado Application - a list of tuples that are (path, handler) or (path: str, handler: RequestHandler, handler_init_kwargs: dict).

@mvirus1996
Copy link

@philippjfr
Can you explain how to redirect in this integration.
I tried "redirect()" function of flask but not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants