pywebio / PyWebIO

Write interactive web app in script way.
https://pywebio.readthedocs.io
MIT License
4.54k stars 383 forks source link

multiple routes #82

Closed Shahin-rmz closed 3 years ago

Shahin-rmz commented 3 years ago

Hello again, what is the best and easiest way to have multiple routes app? Tried to use flask with app.add_url_rule() but for multiple routes got the error

webio_view() can not be over write.

Thanks

Shahin-rmz commented 3 years ago

image BTW, in this part of documentation, the first part is named

task 1, task 2

but the example is A and B.

wang0618 commented 3 years ago

Multiple routes app

The start_server() and path_deploy() both support multiple app.

start_server() accepts a list of task function or a dictionary of task function, and path_deploy() can route the PyWebIO applications by request path.


If you want to blind multiple app to your Flask project, you can use the follow code:

from pywebio.platform.flask import webio_view
from flask import Flask

app = Flask(__name__)

# `task_func` is PyWebIO task function
app.add_url_rule('/tool', 'webio_view', webio_view(task_func), methods=['GET', 'POST', 'OPTIONS']) 
app.add_url_rule('/tool2', 'webio_view', webio_view(task_func2), methods=['GET', 'POST', 'OPTIONS'])  
app.run(host='localhost', port=80)

Also, webio_view() accepts a list of task function or a dictionary of task function:

from pywebio.platform.flask import webio_view
from flask import Flask

app = Flask(__name__)
app.add_url_rule('/tool', 'webio_view', webio_view([task_func, task_func2]), methods=['GET', 'POST', 'OPTIONS']) 
app.run(host='localhost', port=80)

When pass multiple task functions to webio_view(), you need use app url parameter to route the app. e.g. using http://host:port/tool?app=task_func2 to access task_func2 app.

wang0618 commented 3 years ago

image BTW, in this part of documentation, the first part is named

task 1, task 2

but the example is A and B.

Two ways to start server mode is introduced in that part of documentation. The first example is about start_server(), and the second is the usage of path_deploy()

Shahin-rmz commented 3 years ago

@wang0618 Thank you for the detailed answer. I do believe that the Pywebio has the potential to become greater than Streamlit and Plotly Dash. It has the simplicity of Streamlit and functionality of Dash. I ,as a medical student, will do my best, to give the credit of my projects to PyWebIO and make it even more famous in German speaking world. Keep doing support. Best.

Shahin-rmz commented 3 years ago

File "", line 27, in
File "/home/PATH/python3.8/site-packages/flask/app.py", line 98, in wrapper_func
┊ return f(self, *args, **kwargs)
File "/home/PATHlib/python3.8/site-packages/flask/app.py", line 1282, in add_url_rule
┊ raise AssertionError(
AssertionError: View function mapping is overwriting an existing endpoint function: webio_view Here is the error which I got.


from pywebio.platform.flask import webio_view
from pywebio import STATIC_PATH
from flask import Flask, send_from_directory
from pywebio.input import input, FLOAT
from pywebio.output import put_text

app = Flask(name)

def bmi(): height = input("your height in cm: ",type = FLOAT) weight = input("your weight in kg : ", type = FLOAT)

def mba(): Height2 = input ("22222 in cm", type = FLOAT) Weight2 = input("3333333: ", type = FLOAT)

app.add_url_rule('/home', 'webio_view', webio_view(bmi), methods=['GET', 'POST', 'OPTIONS'])
app.add_url_rule('/home2', 'webio_view', webio_view(mba), methods=['GET', 'POST', 'OPTIONS']) app.run(host='localhost', port=5000)


here is the code!
simple but does not work!
wang0618 commented 3 years ago

Oops! Forgot to change the second parameter of app.add_url_rule, you need a different name in app.add_url_rule:

app.add_url_rule('/home', 'webio_view1', webio_view(bmi), methods=['GET', 'POST', 'OPTIONS'])  
app.add_url_rule('/home2', 'webio_view2', webio_view(mba), methods=['GET', 'POST', 'OPTIONS']) 

See also: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.add_url_rule

Shahin-rmz commented 3 years ago

Works smoothly and without any problems. End of the issue. Thanks.