GithubHelp home page GithubHelp logo

flask-script's Introduction

Deprecated

While the maintainers are willing to merge PR's, they are not actively developing features. As of Flask 0.11, Flask includes a built-in CLI tool, and that may fit your needs better.

Flask-Script

image

Latest Version

Supported Python versions

License

A set of utilities for use with the Flask framework which provide decorators, classes and helpers for writing your own script commands.

Useful for creating command-line scripts, cronjobs etc outside your web application.

Resources

flask-script's People

Contributors

anthraxx avatar archugs avatar bsandrow avatar ctacyok avatar dag avatar ento avatar hellysmile avatar item4 avatar jeffwidman avatar jinpark avatar jirikuncar avatar klen avatar lepture avatar ma233 avatar mchaput avatar miguelgrinberg avatar nfvs avatar peterdemin avatar r-m-n avatar rduplain avatar ronnypfannschmidt avatar rvoicilas avatar smlynch avatar smurfix avatar takistakis avatar techniq avatar thomaswaldmann avatar timster avatar turbo87 avatar vladimirpal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-script's Issues

--processes n on Windows

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.

Distribute entry points

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.)


Odd Option Behavior

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.

Shell command fails when IPython>=0.11 is installed

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'

Shell autoreload

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.

prompt_* should not add question mark

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!

Update documentation for threading/processes.

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.

Flask-Script should not require an app instance

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()

IPython not using default profile

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 ;)

Sorted commands

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


0.6.1 fails to embed ipython at all

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'.

Shell doesn't respect -B

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.


IPython Shell embedding fails after upgrade to IPython 1.0

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)

Do not catch exceptions; support for DEBUG

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.


Make it possible to integrate externally written subcommands

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.


Better support for derived commands

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.


Add support for alternate formatting classes

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

@manager.command vs @manager.option seems strange

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"))

Exception KeyError: KeyError(4414541008,) in <module 'threading'

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.

Server needs commandline options for threaded and processes werkzeug server options

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).

Little typo in documentation on readthedocs site

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

Changelog

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!

Release with python3 support

Looks like the python3 branch already have a python3 support. Can you make a release the package for users who uses python3?

0.5.3 to 0.6.x

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'

Error when the app is a "combined app" using werkzeug's wsgi.DispatcherMiddleware

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,

Use proper argparse subparsers

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.

unicode cmdline / console IO support

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


Python 3.3 issues?

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

The debug option does not work

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.

running stand-alone script within flask context

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.