smurfix / flask-script Goto Github PK
View Code? Open in Web Editor NEWFlask extension to help writing external scripts for Flask applications
License: Other
Flask extension to help writing external scripts for Flask applications
License: Other
It would be useful to be able to specify command-line options to be passed to the application factory. For example, a '-c/--config' option to pass a configuration file on the command line.
prompt_bool should have additional arguments for yes/no choices (with existing choices as default). This would be helpful for non-English speaking users.
Looks like the python3
branch already have a python3 support. Can you make a release the package for users who uses python3?
The debug is being set to True no matter what setting is being passed in config.
Hi,
I'm new to flask-script/github. Is there something similar to django-shell in flask script?
https://github.com/generalov/django-shell
Thanks!
Currently a question mark is added by prompt_bool() and prompt_choices() after the given prompt. Although this saves a character, it makes it impossible to provide a question without the question mark (like in other languages with different punctuation). Furthermore, I found it unintuitive to not write my question in full as I expect it to be presented to the user, and I guess a source reviewer will find my string without a question mark suspicious. Please consider not adding punctuation to the given prompt.
Thanks for flask-script!
after upgrading to 0.6.x i get
Traceback (most recent call last):
File "manage.py", line 119, in <module>
manager.run()
File "/Users/locojay/.virtualenvs/hamss/lib/python2.7/site-packages/flask_script/__init__.py", line 366, in run
raise e
TypeError: create_parser() got an unexpected keyword argument 'parents'
I'm trying to subclass flaskext.script.Server and have run into several problems. The splitting of option specifications between init, get_options and run doesn't seem to make much sense. I can't easily override individual options from the superclass (e.g., making host default to 0.0.0.0 instead of 127.0.0.1). This is the best I could figure out:
#!python
class Record(Server):
description = 'records responses from the Xxxx data service'
def __init__(self, host='0.0.0.0', **kw):
super(Record, self).__init__(host, **kw)
def get_options(self):
return (super(Record, self).get_options() +
(Option('-o', '--xxxx-host',
dest='xxxx_host', required=True),))
def run(self, app, xxxx_host, *args, **kw):
app.config.update(XXXX_HOST=xxxx_host, PLAYBACK=False)
super(Record, self).run(app, *args, **kw)
but it isn't exactly pretty.
A patch to sort commands:
--- a/flaskext/script.py Sun Aug 15 22:16:14 2010 +0100
+++ b/flaskext/script.py Mon Aug 30 19:36:18 2010 +0200
@@ -575,7 +575,7 @@
rv = []
for name, command in self._commands.iteritems():
for name, command in sorted(self._commands.iteritems()):
usage = name
description = command.description or ''
usage = format % (name, description)
Davide Muzzarelli
www.dav-muz.net
Whatever happened to:
http://bitbucket.org/danjac/flask-script/changeset/602d63f80d61
Currently, Flask-Script ignores all whitespace formatting when displaying help text for a command. It would be great if we could specify which one of the argparse formatter classes to use in formatting the help text.
Currently I have help text for my command and it prints out like the following:
Send out email task reminders to active employees If the threshold argument is
None (the default), the total percentage of the company about to be emailed
will be printed and the user will be prompted as to whether or not they wish
to continue. When set to a value, it must be set to a decimal value equal to
or less than 1.0. If greater than or equal to 0.0, the percentage of people to
be emailed will be checked against the threshold value and if it doesn't
exceed it, the emails will be sent. Setting the value to a negative number,
turns off threshold checking completely. So, when running non-interactively
(i.e., as a cron job) the threshold argument must be set. Examples: Set the
threshold to 50% of the company: ./bin/cron-jobs send_task_reminders -t 0.5
Turn threshold checking off: ./bin/cron-jobs send_task_reminders -t -1 Keyword
Arguments: username -- the user to notify threshold -- the percentage of the
company below which we can safely email
When really it should look like the following if I add the formatter_class argument to the argparse.ArgumentParser constructor and give it either argarse.RawTextHelpFormatter or argparse.ArgumentDefaultsHelpFormatter.
Send out email task reminders to active employees
If the threshold argument is None (the default), the total percentage of the
company about to be emailed will be printed and the user will be prompted as
to whether or not they wish to continue. When set to a value, it must be set
to a decimal value equal to or less than 1.0. If greater than or equal to 0.0,
the percentage of people to be emailed will be checked against the threshold
value and if it doesn't exceed it, the emails will be sent. Setting the value
to a negative number, turns off threshold checking completely. So, when
running non-interactively (i.e., as a cron job) the threshold argument must
be set.
Examples:
Set the threshold to 50% of the company:
./bin/cron-jobs send_task_reminders -t 0.5
Turn threshold checking off:
./bin/cron-jobs send_task_reminders -t -1
Keyword Arguments:
username -- the user to notify
threshold -- the percentage of the company below which we can safely email
Hey there Sean, hope you're having a great week :)
I found a minor typo in the docs:
Under title Note to extension developers
In the example code:
manager = Manager("Perform database operations")
should read
manager = Manager(usage="Perform database operations",
with_default_commands=False)
Also, I wonder if the usage should be ommitted when using the sub-command:
e.g.
(flask-pypy)fots@fotsies-ubprecise-01:~/flask-fots-templates/force-template$ ./manage.py
Please provide a command:
clean Remove *.pyc and *.pyo files in the project
cleandb Drops all tables in the database
config Display effective running configuration of the application
db Perform database operations
pep8 Validates all Python source files against the PEP8 standard
runcherry Runs a CherryPy development server
runrocket Runs a Rocket development server (without auto-reload)
runserver Runs the Flask development server i.e. app.run()
runtornado Runs a Tornado development server
shell Runs a Python shell inside Flask application context.
syncdb Creates all tables in the database
test Run application tests
urls Displays all of the url matching routes for the project.
(flask-pypy)fots@fotsies-ubprecise-01:~/flask-fots-templates/force-template$ ./manage.py db
Please provide a command:
Perform database operations <-- this line looks a bit out of place here, whatcha think?
hello Say hello
It looks a bit neater in the docs due to the spacing, perhaps it could be:
(flask-pypy)fots@fotsies-ubprecise-01:~/flask-fots-templates/force-template$ ./manage.py db
Perform database operations
Please provide a command:
hello Say hello
Thanks a lot
Fotis
I would like to have runserver do a reload by default, but have a flag to disable it (needed for example when running it in an IDE debugger).
Any interest in making this an option in the official version?
Something like @manager.command("...") equivalent to manager.add_command("...", CommandClass()) would make sense (on Python 2.6+, of course) and be analogous to how Flask does URL routing.
Would be nice to be able to add to the usage help for the manager itself, something like:
manager = Manager(app, usage=__doc__)
The added usage string would be printed before the list of commands, whenever that gets printed.
You need app.debug = True or the equivalent in run() in order to enable the Werkzeug debugger in Flask. flaskext.script.Server does not offer this option (of course, I can add app.debug = True in a subclass).
What happend to commit?
https://bitbucket.org/asksol/flask-script/changeset/602d63f80d61
You pulled it, but now it doesn't work anymore.
This breaks Flask-Celery.
I will have make Flask-Celery force install an older version until this is fixed.
Thanks, in advance!
Hi,
I have a small internal Flask application.
Today I upgraded some packages among which Flask-Script (to version 0.6) and IPython (to the newly released 1.0 version).
When I tried to start an interactive shell via
$ ./manage.py shell
I received the following error:
Traceback (most recent call last):
File "./manage.py", line 83, in <module>
manager.run()
File "/srv/envs/intranet/local/lib/python2.7/site-packages/flask_script/__init__.py", line 366, in run
raise e
AttributeError: 'module' object has no attribute 'frontend'
After some digging around in commands.py
on line 258 I saw the following line:
sh = IPython.frontend.terminal.embed.InteractiveShellEmbed(banner1=self.banner)
I simply removed frontend
and changed the line to the following:
sh = IPython.terminal.embed.InteractiveShellEmbed(banner1=self.banner)
After that everything worked correctly.
I suppose you need to add a check for the installed version of IPython and if it is 1.0 or newer then use the new way of embedding the IPython shell or maybe simply checking if the module has a 'frontend' attribute.
E.g.
import IPython
if hasattr(IPython, 'frontend'):
sh = IPython.frontend.terminal.embed.InteractiveShellEmbed(banner1=self.banner)
else:
sh = IPython.terminal.embed.InteractiveShellEmbed(banner1=self.banner)
When starting a server with runserver -d
the Flask app is not actually put in debug mode. The option is not transferred to app.debug
. The reload option -r
is though.
Problem:: Using an python debugger (e.g. IDE) exceptions should not be cached by the flask/werkzeug framework.
Situation:: //use_debugger// and //use_reloader// are not enought to disable PON'T PANIC support in werkzeug
Solution:: Use the //DEBUG=True// configuration parameter to advice flask-script to disable all exceptions handlers in flask and werkzeug. See the attached patch.
From #30, @ThomasWaldmann notes:
As IE and chrome incognito mode seem to have a similar behaviour, you just made these users happy. Well, in case they find out that they have to use these options... - maybe it should be documented somewhere (where?) that one should try --threaded or --processes N if one experiences a "hanging" server. It maybe also should be documented that a non-threaded / single process server is easier for debugging purposes.
With Flask-Script is on active development mode again I think it's good idea to store all changes between versions somewhere in docs.
Why changelog needed in my case? I've seen that issue with reading debug properties for app on running server is fixed, switched to 0.5.0 version, but not found change there. So, i had to swtich again, now to master tarball for getting this fix.
Anyway, thanks for your work!
we are using flask-script in moin2 and recently found an issue when trying to give non-ascii user or page names via cmdline.
Even if Option(..., type=unicode, ...) it does not work because this just does unicode(s) and falls over everything the ascii decoder can't handle.
I tried to fix this, but later it seemed like opening a can of worms:
a) it seems to be not easy to find out the console encoding in a platform-independant reliable way
b) on windows (as usual), there seem to be more issues even requiring ctypes based workarounds (there was some discussion in the python bugtracker, but slow going - and it doesn't look like they want to fix it for python 2.x [and even if they'ld fix it now, it wouldn't be available for most existing python installations])
http://bugs.python.org/issue2128
The best place to fix this would be in argparse, but as that is part of Python stdlib now, the above issue applies. The separately available argparse seems stale.
I thought about fixing this in moin2, but it is the worst place. A little more useful (if not fixable in argparse) would be a fix in flask-script.
c) not cmdline, but closely related: after having solved a), supporting unicode input/output from console would also be a nice thing to have
Hi,
Someone has added an auto-reloading functionality to the python REPL (http://benplesser.com/2013/01/10/beefing-up-the-python-shell-to-build-apps-faster-and-dryer/ thanks for his work) allowing an auto-reload of the import, and even to swap already existing objects.
I've already ported it to be used with Flask-Script (https://github.com/Kazy/flask-script), but I would like to know if you're interested to integrate it. It has been set to be activated by default, unless watchdog is not available.
I like to keep the manager in my app package and use distribute entry points:
# myapp/manage.py
def main():
manager.run()
# setup.py
entry_points={
'console_scripts': [
'myappctl = myapp.manage:main',
],
},
The downside is that it relies on distribute and for your app to be installed or pseudoinstalled (developer mode) but the upside is that it lets your app actually work like a proper distributable package, not relying on being in the source directory or even even having the source directory. Just install and use myappctl. I also like having code related to my package //in// my actual package.
Often you might just have development stuff in your manage.py but maybe you have deployment stuff such as creating tables in a database etc. This way you can just put an sdist with fabric, easy_install it and your manager is just there.
Maybe this method should be documented as an alternative?
(Even without entry points, this still lets you do python -m myapp.manage
outside of the source dir or python myapp/manage.py
inside it.)
See here for a detailed explanation from an IPython developer as to why embed() should not be used for this type of application:
Basically, embed() causes issues where custom ipython configuration settings are not loaded correctly.
#!python
@manager.command
def create_user(username):
print type(username)
This outputs <type 'str'>. Since flask script knows which encoding the terminal is using, I believe it should provide me with an unicode object.
Hi,
This is may not be a "real bug" but I'm getting the following error when i create the Manager object with a "combined app" - created using werkzeug's DispatcherMiddleware
// ie. app = wsgi.DispatcherMiddleware(app1, {'/app2': app2})
manager = Manager(app)
File "../venv/lib/python2.7/site-packages/flask_script/__init__.py", line 366, in run
raise e
TypeError: __call__() takes exactly 3 arguments (1 given)
Again, this may be by design or something i haven't understood.
Thanks in advance,
Shell and server options are missing help hints.
With the example script, try:
#!shell
python manage.py optional --help
Note that the name/url arguments don't show up.
I've just run into a command which raised an IndexError, which mysteriously resulted in Flask-Script outputing it's help, while swalling the actual exception.
This is due to the way Manager.run() validates the correct number of items in argv.
The check for argv should instead be written as an if-condition.
Hope this is not the wrong place to post a question. Thanks!
I'd suggest just having Option arguments to manager.command, i.e., instead of:
#!python
@manager.option('-n', '--name', dest='name', help="your name")
@manager.option('-u', '--url', dest='url', help="your url")
have:
#!python
@manager.command(Option('-n', '--name', dest='name', help="your name")),
Option('-u', '--url', dest='url', help="your url"))
I've been having some trouble with the Shell command.
I normally use the -B flag on the interactive interpreter during development to prevent .pyc's from making a mess, and to ease the use of reload().
But when using the following:
python -B manage.py shell
any import done from within the spawned shell creates .pyc files.
Environment:
Python 2.7.5
IPython 0.13.2
flask-script 0.6 and 0.6.1
With IPython 0.13 everything works fine in flask-script 0.6, Shell command launches ipython as expected.
After upgrading to 0.6.1 Shell command launches ordinary Python shell. No errors, stack traces, just plain Python shell instead of IPython.
IPython 1.0 behaves exactly as 0.13 under 0.6.1, fails under 0.6 with AttributeError: 'module' object has no attribute 'frontend'.
You don't import Command in the first example.
And the source/issues bitbucket link is broken, the URL should be lowercase.
http://paste.pocoo.org/show/568845/
Traceback (most recent call last):
File "C:\Python26\Lib\SocketServer.py", line 281, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python26\Lib\SocketServer.py", line 523, in process_request
pid = os.fork()
AttributeError: 'module' object has no attribute 'fork'
I cannot test and confirm it. Though I can try to fix it by forbidding to use this option on windows, but still cannot test and confirm the fix.
$ stutuzctl shell -h
usage: /home/dag/.virtualenvs/stutuz/bin/stutuzctl shell [-h]
Not very pretty.
Hi,
I'm currently running stand-alone scripts with the command below:
@manager.command
def sh(file_path):
print file_path
path = os.path.join(project_root_path, file_path)
v = """echo 'execfile("%s")' | python""" % path
subprocess.call(v, shell=True)
This doesn't work very well. Is there another way to run stand-alone scripts within the flask context?
I'm using Flask-Script==0.5.2 (latest stable), and am having some issues with the following command:
@manager.command
def blast(template, subject, from_addr='[email protected]', from_name='Blah'):
...
This command is supposed to require a template, subject, and allow an optional from address and from name to be specified on the CLI. What happens when I run this, however, (specifying only the first two required arguments), is:
Traceback (most recent call last):
File "manage.py", line 334, in <module>
manager.run()
File "/home/rdegges/.virtualenvs/accounts/local/lib/python2.7/site-packages/flask_script/__init__.py", line 351, in run
result = self.handle(sys.argv[0], command, sys.argv[2:])
File "/home/rdegges/.virtualenvs/accounts/local/lib/python2.7/site-packages/flask_script/__init__.py", line 315, in handle
command_parser = command.create_parser(prog + " " + name)
File "/home/rdegges/.virtualenvs/accounts/local/lib/python2.7/site-packages/flask_script/commands.py", line 133, in create_parser
parser.add_argument(*option.args, **option.kwargs)
File "/usr/lib/python2.7/argparse.py", line 1295, in add_argument
return self._add_action(action)
File "/usr/lib/python2.7/argparse.py", line 1669, in _add_action
self._optionals._add_action(action)
File "/usr/lib/python2.7/argparse.py", line 1496, in _add_action
action = super(_ArgumentGroup, self)._add_action(action)
File "/usr/lib/python2.7/argparse.py", line 1309, in _add_action
self._check_conflict(action)
File "/usr/lib/python2.7/argparse.py", line 1447, in _check_conflict
conflict_handler(action, confl_optionals)
File "/usr/lib/python2.7/argparse.py", line 1454, in _handle_conflict_error
raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument -f/--from_addr: conflicting option string(s): -f
I've tried specifying the arguments numerous ways, eg:
$ python manage.py blast 'template' 'subject'
$ python manage.py blast --template='template' --subject='subject'
$ python manage.py blast 'template' 'subject' 'from_addr' 'from_name'
However, regardless of how I specify the options, I get the same error.
Stacktrace:
sef$ python manage.py shell
Traceback (most recent call last):
File "manage.py", line 11, in <module>
manager.run()
File "/Users/sef/.virtualenvs/v1/lib/python2.7/site-packages/flaskext/script.py", line 684, in run
sys.argv[2:])
File "/Users/sef/.virtualenvs/v1/lib/python2.7/site-packages/flaskext/script.py", line 655, in handle
command.handle(app, *positional_args, **command_namespace.__dict__)
File "/Users/sef/.virtualenvs/v1/lib/python2.7/site-packages/flaskext/script.py", line 192, in handle
self.run(*args, **kwargs)
File "/Users/sef/.virtualenvs/v1/lib/python2.7/site-packages/flaskext/script.py", line 283, in run
sh = IPython.Shell.IPShellEmbed(banner=self.banner)
AttributeError: 'module' object has no attribute 'Shell'
Currently, Flask-script always uses sys.argv[1]
as the command arguments. This is somewhat annoying, especially if you add a global option -c, --config to specify a script on the command line. Consider this:
./manage.py -c dev.conf create_user foo
Looks quite natural, all options before create_user apply to the app itself, all after create_user to the command. Argparse supports this through subparsers.
Unfortunately, the current implementation requires this:
./manage.py create_user -c dev.conf foo
or
./manage.py create_user foo -c dev.conf
These two are incredibly annoying - there's no way to alias or reuse a commandline, because in both cases you have to edit it somewhere in the middle to change the command. Using the better way described up top, Flask-Script will complain that it doesn't know the command -c. This is mainly because the extension is not properly using argparse, but rather creating multiple parsers on the fly and using a custom crutch to handle commands.
I would like to integrate the the sqlalchemy-migrate command into my manage.py script. sqlalchemy-migrate implements a main() function which I can pass an argv list, and it'll do it's own option parsing on it.
Now, say if Flask-Script would allow commands to specify an option (like a class-level attribute maybe) to cause parse_known_args() to be used instead of the strict parse_args() when handling the command arguments, and would then pass on the remaining args to the command.
I could then easily pass them on to the sqlalchemy-migrate command, making it seem like they come from the command line direct.
Server.get_options at one point defines an Option with "use_debugger" as a target, where it should be "use_runserver".
The IPython-based shell in recent ipython versions does not load the default ipython profile, which results in e.g. the "do you really want to exit" prompt (which is as annoying as vista's UAC prompts ;)) being always enabled.
From the #ipython channel:
<ThiefMaster> is it possible to disable this annoying exit prompt for *all* ipython shells, i.e. also the embedded ones used by programs such as flask-script? for the old ipython disabling it in ipythonrc worked fine, but in the new version c.TerminalInteractiveShell.confirm_exit = False only affects the standalone ipython
<jtaylor> it depends on how the embeded shell is started
<jtaylor> probably flask does not import the default profile
<jtaylor> so you probably have to modify flask
<ThiefMaster> try:
<ThiefMaster> sh = IPython.Shell.IPShellEmbed(banner=self.banner)
<ThiefMaster> except AttributeError:
<ThiefMaster> sh = IPython.frontend.terminal.embed.InteractiveShellEmbed(banner1=self.banner)
<ThiefMaster> and ugh, that sounds like a huge step backwards..
<jtaylor> IPython.embed() uses the default source
<jtaylor> see IPython.embed?? for how its done
<jtaylor> ths issue is for many embedded shells you don't want the default profile as e.g. could execute a bunch of files for convinience in the default shell
<jtaylor> that make no sense when used in some other program
I think it would make sense for flask-script to load the default profile or at least provide a convenient option to modify the ipython config used by it. Or simply disable the exit prompt ;)
For example:
% python manage.py output --help
usage: manage.py [-h] name
note the lack of "output" in the usage message.
See https://bitbucket.org/thomaswaldmann/moin-2.0/issue/36/ie9-and-chrome-incognito-hang-the-built-in about the issues caused by a single-threaded, single process server.
Reproducable also like this: start some app with the builtin server, then telnet localhost portnumber (and don't type anything, just let telnet keep the connection open), then try to access the app with the browser -> hangs.
As the server is not multithreaded/multitasking, that has to be expected, of course.
Having threading / multiprocess by default might be an annoyance for development, so I suggest adding commandline options for the Server object, so the user/developer can decide.
The werkzeug server defaults to: threaded=False, processes=1
So I suggest --threaded to switch that to True (keep False as default) and --processes=N to have N processes (keep 1 as default).
It would be much better if flask-script would require a app_factory method instead of an actual app instance. Doing so, flask-script would play "better" with other extensions. Example:
#!python
from flask_ext.lettuce import Harvest
from flask_ext.script import Manager
from flask import Flask
def app_factory(config):
app = Flask(__name__)
app.config.from_object(config)
manager = Manager(lambda: app_factory(dev_config))
manager.add_command("harvest", Harvest(lambda: app_factory(test_config)) )
manager.run()
After I run any flash CLI script, I get this:
Exception KeyError: KeyError(4414541008,) in <module 'threading' from '/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored
I have Python 2.7.3 installed from Brew on my OS X 10.8.
Doing some python 3.3 testing, and the first error I get is with flask-script:
File "manage_producer.py", line 4, in <module>
from flask.ext.script import Manager, Server, Shell
File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
File "<frozen importlib._bootstrap>", line 1525, in _find_and_load_unlocked
File "$HOME/.virtualenvs/j3/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module __import__(realname)
File "$HOME/.virtualenvs/j3/lib/python3.3/site-packages/flask_script/__init__.py", line 286
print self.get_usage()
^
SyntaxError: invalid syntax
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.