-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements #24 support for serving multiple click scripts.
- Loading branch information
1 parent
d98693d
commit cad1bac
Showing
8 changed files
with
160 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
This example shows how to serve multiple click scripts at the same time under different url paths. | ||
It uses Flask DispatcherMiddleware in order to route to each individual Flask app for each Click command and provides a | ||
hard coded index page to reach them. | ||
""" | ||
from flask import Flask, render_template_string | ||
from werkzeug.middleware.dispatcher import DispatcherMiddleware | ||
|
||
from click_web import create_click_web_app | ||
from example import example_command | ||
from example.multi_app import example_command2 | ||
|
||
# Create the main Flask app | ||
app = Flask(__name__) | ||
|
||
|
||
# Create the index route in the main app | ||
@app.route('/') | ||
def index(): | ||
commands = [ | ||
{'name': 'Command 1', 'path': '/command1'}, | ||
{'name': 'Command 2', 'path': '/command2'}, | ||
] | ||
return render_template_string(''' | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>Available Commands</title> | ||
</head> | ||
<body> | ||
<h1>Available Commands</h1> | ||
<ul> | ||
{% for cmd in commands %} | ||
<li><a href="{{ cmd.path }}">{{ cmd.name }}</a></li> | ||
{% endfor %} | ||
</ul> | ||
</body> | ||
</html> | ||
''', commands=commands) | ||
|
||
|
||
# Create individual Flask apps for each Click command | ||
app1 = create_click_web_app(example_command, example_command.cli) | ||
app2 = create_click_web_app(example_command2, example_command2.cli) | ||
|
||
# Wrap the original app.wsgi_app with DispatcherMiddleware | ||
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, { | ||
'/command1': app1, | ||
'/command2': app2 | ||
}) | ||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import time | ||
|
||
import click | ||
|
||
DEBUG = False | ||
|
||
|
||
@click.group() | ||
@click.option("--debug/--no-debug", help='set debug flag') | ||
def cli(debug): | ||
'Another example click script to test click-web' | ||
global DEBUG | ||
DEBUG = debug | ||
|
||
|
||
@cli.command() | ||
@click.option("--delay", type=float, default=0.01, required=True, help='delay between each print line') | ||
@click.option("--message", type=click.Choice(['Red', 'Blue']), default='Blue', required=True, | ||
help='Message to print.') | ||
@click.argument("lines", default=10, type=int) | ||
def print_lines(lines, message, delay): | ||
"Just print lines with delay (demonstrates the output streaming to browser)" | ||
if DEBUG: | ||
click.echo("global debug set, printing some debug output") | ||
click.echo(f"writing: {lines} lines with delay {delay}s") | ||
for i in range(lines): | ||
click.echo(f"{message} row: {i}") | ||
time.sleep(delay) | ||
|
||
|
||
if __name__ == '__main__': | ||
cli() |
Oops, something went wrong.