GithubHelp home page GithubHelp logo

introtoflask's Introduction

Intro to Flask

Python Dependendies

Make sure that Flask is installed:

% sudo pip install Flask

If you are using the Anaconda Python distribution, Flask is already installed; you can make sure you are running the current version with:

% sudo conda update flask

This tutorial has these Python dependencies:

  • Flask-JSGlue - this lets me use Flask url_for inside javascript
  • Flask-Testing - this makes it (a bit) easier to unit test your app

Install them:

% sudo pip install Flask-JSGlue
% sudo pip install Flask-Testing

Follow along with the code and the IntroToFlask slides.

Add the following to your PYTHONPATH in your .bashrc, .profile, or .tschrc

export PYTHONPATH=$PYTHONPATH:/your/path/to/this/repo/introtoflask/myapp

and replace your/path/to/this/repo with your directory path. Do this if you want your app to behave as a Python package, and to be able to import parts of it here and there.

A Simple Example

This single file appliation demonstrates basic concepts of Flask. To run, simply type:

python app.py

I've added a few command-line arguments to make it a bit more flexible:

-d --debug - runs the app in debug mode
-p --port [number] - specify a different port to run the app on other than the default port 5000

Try:

python app.py -d -p 8000

In debug mode, the Flask app will auto-reload when it detects code changes, and provides full tracebacks in the browser for easy debugging and development. Let's work in debug mode from here on!

In the browser, now navigate to http://localhost:8000/

A More Complex Application

This example demonstrates a possible layout for a full-fledged application. The application is now packaged into a module called myapp.

Try:

python run_myapp.py -d -p 9000

The main Flask app is now defined inside a method called create_app, located within the module's init file. This allows you to run many instances of the same application, using the run_app command.

create_app is a way of creating an 'application factory'. It allows you to test running your app under different configurations

In the browser, now navigate to http://localhost:9000/myapp/

Resources

introtoflask's People

Contributors

marvin-manga avatar havok2063 avatar

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.