├── .gitignore ├── README.md ├── ajax-flask-demo-env ├── bin │ ├── activate │ ├── activate.csh │ ├── activate.fish │ ├── activate_this.py │ ├── easy_install │ ├── easy_install-2.7 │ ├── pip │ ├── pip2 │ ├── pip2.7 │ ├── python │ ├── python2 │ └── python2.7 └── include │ └── python2.7 ├── app.py ├── requirements.txt └── templates └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example jQuery AJAX Post to Python Flask route 2 | 3 | This is an example of how to use the jQuery $.ajax methods to send 4 | JSON data to a Python Flask route. 5 | 6 | $ virtualenv ajax-flask-demo-env 7 | $ source ajax-flask-demo-env/bin/activate 8 | (ajax-flask-demo-env) $ pip install -r requirements.txt 9 | (ajax-flask-demo-env) $ python app.py 10 | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit 11 | * Restarting with stat 12 | * Debugger is active! 13 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/activate: -------------------------------------------------------------------------------- 1 | # This file must be used with "source bin/activate" *from bash* 2 | # you cannot run it directly 3 | 4 | deactivate () { 5 | unset pydoc 6 | 7 | # reset old environment variables 8 | if [ -n "$_OLD_VIRTUAL_PATH" ] ; then 9 | PATH="$_OLD_VIRTUAL_PATH" 10 | export PATH 11 | unset _OLD_VIRTUAL_PATH 12 | fi 13 | if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then 14 | PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME" 15 | export PYTHONHOME 16 | unset _OLD_VIRTUAL_PYTHONHOME 17 | fi 18 | 19 | # This should detect bash and zsh, which have a hash command that must 20 | # be called to get it to forget past commands. Without forgetting 21 | # past commands the $PATH changes we made may not be respected 22 | if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then 23 | hash -r 2>/dev/null 24 | fi 25 | 26 | if [ -n "$_OLD_VIRTUAL_PS1" ] ; then 27 | PS1="$_OLD_VIRTUAL_PS1" 28 | export PS1 29 | unset _OLD_VIRTUAL_PS1 30 | fi 31 | 32 | unset VIRTUAL_ENV 33 | if [ ! "$1" = "nondestructive" ] ; then 34 | # Self destruct! 35 | unset -f deactivate 36 | fi 37 | } 38 | 39 | # unset irrelevant variables 40 | deactivate nondestructive 41 | 42 | VIRTUAL_ENV="/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env" 43 | export VIRTUAL_ENV 44 | 45 | _OLD_VIRTUAL_PATH="$PATH" 46 | PATH="$VIRTUAL_ENV/bin:$PATH" 47 | export PATH 48 | 49 | # unset PYTHONHOME if set 50 | # this will fail if PYTHONHOME is set to the empty string (which is bad anyway) 51 | # could use `if (set -u; : $PYTHONHOME) ;` in bash 52 | if [ -n "$PYTHONHOME" ] ; then 53 | _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME" 54 | unset PYTHONHOME 55 | fi 56 | 57 | if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then 58 | _OLD_VIRTUAL_PS1="$PS1" 59 | if [ "x" != x ] ; then 60 | PS1="$PS1" 61 | else 62 | if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then 63 | # special case for Aspen magic directories 64 | # see http://www.zetadev.com/software/aspen/ 65 | PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" 66 | else 67 | PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" 68 | fi 69 | fi 70 | export PS1 71 | fi 72 | 73 | alias pydoc="python -m pydoc" 74 | 75 | # This should detect bash and zsh, which have a hash command that must 76 | # be called to get it to forget past commands. Without forgetting 77 | # past commands the $PATH changes we made may not be respected 78 | if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then 79 | hash -r 2>/dev/null 80 | fi 81 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/activate.csh: -------------------------------------------------------------------------------- 1 | # This file must be used with "source bin/activate.csh" *from csh*. 2 | # You cannot run it directly. 3 | # Created by Davide Di Blasi . 4 | 5 | alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate && unalias pydoc' 6 | 7 | # Unset irrelevant variables. 8 | deactivate nondestructive 9 | 10 | setenv VIRTUAL_ENV "/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env" 11 | 12 | set _OLD_VIRTUAL_PATH="$PATH" 13 | setenv PATH "$VIRTUAL_ENV/bin:$PATH" 14 | 15 | 16 | 17 | if ("" != "") then 18 | set env_name = "" 19 | else 20 | if (`basename "$VIRTUAL_ENV"` == "__") then 21 | # special case for Aspen magic directories 22 | # see http://www.zetadev.com/software/aspen/ 23 | set env_name = `basename \`dirname "$VIRTUAL_ENV"\`` 24 | else 25 | set env_name = `basename "$VIRTUAL_ENV"` 26 | endif 27 | endif 28 | 29 | # Could be in a non-interactive environment, 30 | # in which case, $prompt is undefined and we wouldn't 31 | # care about the prompt anyway. 32 | if ( $?prompt ) then 33 | set _OLD_VIRTUAL_PROMPT="$prompt" 34 | set prompt = "[$env_name] $prompt" 35 | endif 36 | 37 | unset env_name 38 | 39 | alias pydoc python -m pydoc 40 | 41 | rehash 42 | 43 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/activate.fish: -------------------------------------------------------------------------------- 1 | # This file must be used with "source bin/activate.fish" *from fish* (http://fishshell.com) 2 | # you cannot run it directly 3 | 4 | function deactivate -d "Exit virtualenv and return to normal shell environment" 5 | # reset old environment variables 6 | if test -n "$_OLD_VIRTUAL_PATH" 7 | set -gx PATH $_OLD_VIRTUAL_PATH 8 | set -e _OLD_VIRTUAL_PATH 9 | end 10 | if test -n "$_OLD_VIRTUAL_PYTHONHOME" 11 | set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME 12 | set -e _OLD_VIRTUAL_PYTHONHOME 13 | end 14 | 15 | if test -n "$_OLD_FISH_PROMPT_OVERRIDE" 16 | # set an empty local fish_function_path, so fish_prompt doesn't automatically reload 17 | set -l fish_function_path 18 | # erase the virtualenv's fish_prompt function, and restore the original 19 | functions -e fish_prompt 20 | functions -c _old_fish_prompt fish_prompt 21 | functions -e _old_fish_prompt 22 | set -e _OLD_FISH_PROMPT_OVERRIDE 23 | end 24 | 25 | set -e VIRTUAL_ENV 26 | if test "$argv[1]" != "nondestructive" 27 | # Self destruct! 28 | functions -e deactivate 29 | end 30 | end 31 | 32 | # unset irrelevant variables 33 | deactivate nondestructive 34 | 35 | set -gx VIRTUAL_ENV "/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env" 36 | 37 | set -gx _OLD_VIRTUAL_PATH $PATH 38 | set -gx PATH "$VIRTUAL_ENV/bin" $PATH 39 | 40 | # unset PYTHONHOME if set 41 | if set -q PYTHONHOME 42 | set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME 43 | set -e PYTHONHOME 44 | end 45 | 46 | if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" 47 | # fish uses a function instead of an env var to generate the prompt. 48 | 49 | # copy the current fish_prompt function as the function _old_fish_prompt 50 | functions -c fish_prompt _old_fish_prompt 51 | 52 | # with the original prompt function copied, we can override with our own. 53 | function fish_prompt 54 | # Prompt override? 55 | if test -n "" 56 | printf "%s%s" "" (set_color normal) 57 | _old_fish_prompt 58 | return 59 | end 60 | # ...Otherwise, prepend env 61 | set -l _checkbase (basename "$VIRTUAL_ENV") 62 | if test $_checkbase = "__" 63 | # special case for Aspen magic directories 64 | # see http://www.zetadev.com/software/aspen/ 65 | printf "%s[%s]%s " (set_color -b blue white) (basename (dirname "$VIRTUAL_ENV")) (set_color normal) 66 | _old_fish_prompt 67 | else 68 | printf "%s(%s)%s" (set_color -b blue white) (basename "$VIRTUAL_ENV") (set_color normal) 69 | _old_fish_prompt 70 | end 71 | end 72 | 73 | set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" 74 | end 75 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/activate_this.py: -------------------------------------------------------------------------------- 1 | """By using execfile(this_file, dict(__file__=this_file)) you will 2 | activate this virtualenv environment. 3 | 4 | This can be used when you must use an existing Python interpreter, not 5 | the virtualenv bin/python 6 | """ 7 | 8 | try: 9 | __file__ 10 | except NameError: 11 | raise AssertionError( 12 | "You must run this like execfile('path/to/activate_this.py', dict(__file__='path/to/activate_this.py'))") 13 | import sys 14 | import os 15 | 16 | old_os_path = os.environ['PATH'] 17 | os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path 18 | base = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 19 | if sys.platform == 'win32': 20 | site_packages = os.path.join(base, 'Lib', 'site-packages') 21 | else: 22 | site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages') 23 | prev_sys_path = list(sys.path) 24 | import site 25 | site.addsitedir(site_packages) 26 | sys.real_prefix = sys.prefix 27 | sys.prefix = base 28 | # Move the added items to the front of the path: 29 | new_sys_path = [] 30 | for item in list(sys.path): 31 | if item not in prev_sys_path: 32 | new_sys_path.append(item) 33 | sys.path.remove(item) 34 | sys.path[:0] = new_sys_path 35 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/easy_install: -------------------------------------------------------------------------------- 1 | #!/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env/bin/python2.7 2 | 3 | # -*- coding: utf-8 -*- 4 | import re 5 | import sys 6 | 7 | from setuptools.command.easy_install import main 8 | 9 | if __name__ == '__main__': 10 | sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) 11 | sys.exit(main()) 12 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/easy_install-2.7: -------------------------------------------------------------------------------- 1 | #!/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env/bin/python2.7 2 | 3 | # -*- coding: utf-8 -*- 4 | import re 5 | import sys 6 | 7 | from setuptools.command.easy_install import main 8 | 9 | if __name__ == '__main__': 10 | sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) 11 | sys.exit(main()) 12 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/pip: -------------------------------------------------------------------------------- 1 | #!/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env/bin/python2.7 2 | 3 | # -*- coding: utf-8 -*- 4 | import re 5 | import sys 6 | 7 | from pip import main 8 | 9 | if __name__ == '__main__': 10 | sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) 11 | sys.exit(main()) 12 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/pip2: -------------------------------------------------------------------------------- 1 | #!/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env/bin/python2.7 2 | 3 | # -*- coding: utf-8 -*- 4 | import re 5 | import sys 6 | 7 | from pip import main 8 | 9 | if __name__ == '__main__': 10 | sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) 11 | sys.exit(main()) 12 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/pip2.7: -------------------------------------------------------------------------------- 1 | #!/Users/Casey/projects/ajax-flask-demo/ajax-flask-demo-env/bin/python2.7 2 | 3 | # -*- coding: utf-8 -*- 4 | import re 5 | import sys 6 | 7 | from pip import main 8 | 9 | if __name__ == '__main__': 10 | sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) 11 | sys.exit(main()) 12 | -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/python: -------------------------------------------------------------------------------- 1 | python2.7 -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/python2: -------------------------------------------------------------------------------- 1 | python2.7 -------------------------------------------------------------------------------- /ajax-flask-demo-env/bin/python2.7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caseydunham/ajax-flask-demo/1eecf7d25ceb71fe3af0680e41d5f508a7c3b510/ajax-flask-demo-env/bin/python2.7 -------------------------------------------------------------------------------- /ajax-flask-demo-env/include/python2.7: -------------------------------------------------------------------------------- 1 | /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/include/python2.7 -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, jsonify 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route('/') 6 | def index(): 7 | return render_template('index.html') 8 | 9 | 10 | @app.route('/api/say_name', methods=['POST']) 11 | def say_name(): 12 | json = request.get_json() 13 | first_name = json['first_name'] 14 | last_name = json['last_name'] 15 | return jsonify(first_name=first_name, last_name=last_name) 16 | 17 | 18 | if __name__ == '__main__': 19 | app.run(debug=True) 20 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.10.1 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Werkzeug==0.11.3 5 | itsdangerous==0.24 6 | wsgiref==0.1.2 7 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
7 | 8 |
9 | 10 |
11 | 12 | 13 | 44 | 45 | 46 | --------------------------------------------------------------------------------