Run Flask App On Mac
The flask.Flask object is a WSGI application, not a server. Flask uses Werkzeug's development server as a WSGI server when you call python -m flask run in your shell. It creates a new WSGI server and then passes your app as paremeter to werkzeug.serving.runsimple.Maybe you can try doing that manually. By default, flask will be using the production environment. By using the development environment, the web app will automatically make changes if we make changes in the code. This command will run the program. If you’re using Mac/Linux, use the command export instead of set to run the Flask app.
Flask App Run Host
Adding Docker to your Python andFlaskdevelopment environmentcan be confusing when you are just getting started with containers. Let'squickly get Docker installed and configured for developing Flask webapplications on your local system.
Our Tools
Make sure to not call your application flask.py because this would conflict with Flask itself. To run the application you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal the application to work with. Create and run a minimal Flask app. In Sublime Text IDE, create a new file in your project folder named app.py. In app.py, add code to import Flask and create an instance of the Flask object.
Since, we haven’t give any specific host and port to flask app, so it will run on default host and port which are 127.0.0.1 and 5000 respectively. If you want to change host & port then you can add these arguments in app.run like. On Mac, Mac comes with python2 installed, so when we install python3, it would be referred to as python3. For those who have used Node.js before, you would know how we run npm init to create a package.json and install our project packages in that directory so we can manage them individually per project. Well, Python follows the. To make a fully running Flask application, it is MUCH easier to use a UNIX based operating system than other operating systems (like Windows). UNIX based operating systems include Mac OS X and Linux distributions. While it is possible to setup and run a Flask applications.
Flask Run Command
This tutorial is written for Python 3. It will work withPython 2 but I have not tested it with thesoon-to-be deprecated 2.7 version.
![Run Run](https://scotch-res.cloudinary.com/image/upload/w_1500,q_auto:good,f_auto/media/8943/tB7bOfmgTXO31mJ25Pes_part-1.jpg)
Docker for Mac is necessary.I recommend the stable release unless you have an explicit purpose for the edgechannel.
Within the Docker container we will use:
- Python 3, specifically theslim-3.6.5 versionfrom Docker Hub
- Flask version 1.0.2
All of the code for the Dockerfile and the Flask app are available open sourceunder the MIT license on GitHub under thedocker-flask-mac directoryof theblog-code-examplesrepository. Use the code for your own purposes as much as you like.
Installing Docker on macOS
We need to install Docker before we can spin up our Docker containers. If youalready have Docker for Mac installed and working, feel free to jump to thenext section.
/is-there-a-google-calendar-mac-app.html. On your Mac,download the Docker Community Edition (CE) for Macinstaller.
Find the newly-downloaded install within Finder and double click on the file.Follow the installation process, which includes granting administrative privilegesto the installer.
Open Terminal when the installer is done. Test your Docker installation with the--version
flag:
If Docker is installed correctly you should see the following output:
/best-gmail-app-mac-os.html. However, it seems that for users who need some help, AirMail's superior customer support is available via web chat. Some users are frustrated with the complications they have when trying to move emails into different folders (the drag and drop apparently isn't as easy as proposed). With the minimal view, your inbox is displayed as a one-column timeline like you would see in something like the Mac Twitter app.
![Flask Flask](https://raw.githubusercontent.com/app-generator/static/master/products/flask-dashboard-material-pro-screen.png)
Note that Docker runs through a system agent you can find in the menu bar.
I have found the Docker agent to take up some precious battery lifeon my Macbook Pro. If I am not developing and need to max battery time I willclose down the agent and start it back up again when I am ready to code.
Now that Docker is installed let's get to running a container and writingour Flask application.
Dockerfile
Docker needs to know what we want in a container, which is where theDockerfile
comes in.
Save the Dockerfile so that we can run our next command with the completedcontents of the file. On the commandline run:
The above docker build
file uses the -t
flag to tag the image withthe name of flaskdock
.
If the build worked successfully we can see the image in with thedocker image ls
command. Give that a try now:
We should then see our tag name in the images list:
Our image is ready to load up as a container so we can write a quickFlask app that we will use to test our environment by running it withinthe container.
Coding A Simple Flask app
Time to put together a super simple 'Hello, World!' Flask web app to testrunning Python code within our Docker container. Within the currentproject directory, create a file named app.py
with the following contents:
The above 7 lines of code (not counting blank PEP8-compliant lines) inapp.pyallow our application to return a simple message when run with theFlask development server.
We need just one more file to specify our Flask
dependency. Createa requirements.txt
file within the same directory as app.py
:
Make sure both the app.py
and requirements.txt
file are saved thenwe can give the code a try.
Running the Container
Now that we have our image in hand along with the Python code in a filewe can run the image as a container with the docker run
command. Executethe following command, making sure to replace the absolute path for thevolume to your own directory.
If you receive the errorpython: can't open file 'app.py': [Errno 2] No such file or directory
thenyou likely forgot to chance /Users/matt/devel/py/flaskdocker
to thedirectory where your project files, especially app.py
, are located.
Everything worked when you see a simple text-based HTTP response like whatis shown above in the screenshot of my Chrome browser.
What's Next?
We just installed Docker and configured a Flask application to run inside acontainer. That is just the beginning of how you can integrate Docker intoyour workflow. I strongly recommend reading theDjango with PostgreSQL quickstartthat will introduce you to Docker Swarm as well as the core Docker containerservice.
Next up take a look at the Docker anddeployment pages for more related tutorials.
Questions? Let me know via a GitHubissue ticket on the Full Stack Python repository,on Twitter@fullstackpythonor @mattmakai.
Do you see a typo, syntax issue or just something that's confusing in thisblog post? Forkthis page's source on GitHuband submit a pull request with a fix orfile an issue ticket on GitHub.
Dependencies¶
These distributions will be installed automatically when installing Flask.
Werkzeug implements WSGI, the standard Python interface betweenapplications and servers.
Jinja is a template language that renders the pages your applicationserves.
MarkupSafe comes with Jinja. It escapes untrusted input when renderingtemplates to avoid injection attacks.
ItsDangerous securely signs data to ensure its integrity. This is usedto protect Flask’s session cookie.
Click is a framework for writing command line applications. It providesthe
flask
command and allows adding custom management commands.
Optional dependencies¶
These distributions will not be installed automatically. Flask will detect anduse them if you install them.
Blinker provides support for Signals.
SimpleJSON is a fast JSON implementation that is compatible withPython’s
json
module. It is preferred for JSON operations if it isinstalled.python-dotenv enables support for Environment Variables From dotenv when running
flask
commands.Watchdog provides a faster, more efficient reloader for the developmentserver.