Giter Site home page Giter Site logo

python-flask-app's Introduction

WARNING: This repository is no longer maintained โš ๏ธ

This repository will not be updated. The repository will be kept available in read-only mode.

IBM Cloud

IBM Cloud platform Apache 2

Create and deploy a Python Flask application

We have applications available for Node.js Express, Go Gin, Python Flask, Python Django, Java Spring, Java Liberty, Swift Kitura, Android, and iOS.

In this sample application, you will create a Python cloud application using Flask. This application contains an opinionated set of files for web serving:

  • public/index.html
  • public/404.html
  • public/500.html

This application also enables a starting place for a Python microservice using Flask. A microservice is an individual component of an application that follows the microservice architecture - an architectural style that structures an application as a collection of loosely coupled services, which implement business capabilities. The microservice exposes a RESTful API matching a Swagger definition.

Steps

Deploying to IBM Cloud

IBM Cloud DevOps services provides toolchains as a set of tool integrations that support development, deployment, and operations tasks inside IBM Cloud.

Building locally

To get started building this application locally, you can either run the application natively or use the IBM Cloud Developer Tools for containerization and easy deployment to IBM Cloud.

Native application development

Running Flask applications has been simplified with a manage.py file to avoid dealing with configuring environment variables to run your app. From your project root, you can download the project dependencies with (NOTE: If you don't have pipenv installed, execute: pip install pipenv):

pipenv install

Then, activate this app's virtualenv:

pipenv shell

To run your application locally, run this inside the virtualenv:

python manage.py start

manage.py offers a variety of different run commands to match the proper situation:

  • start: starts a server in a production setting using gunicorn.
  • run: starts a native Flask development server. This includes backend reloading upon file saves and the Werkzeug stack-trace debugger for diagnosing runtime failures in-browser.
  • livereload: starts a development server via the livereload package. This includes backend reloading as well as dynamic frontend browser reloading. The Werkzeug stack-trace debugger will be disabled, so this is only recommended when working on frontend development.
  • debug: starts a native Flask development server, but with the native reloader/tracer disabled. This leaves the debug port exposed to be attached to an IDE (such as PyCharm's Attach to Local Process).

There are also a few utility commands:

  • build: compiles .py files within the project directory into .pyc files
  • test: runs all unit tests inside of the project's test directory

Your application is running at: http://localhost:3000/ in your browser.

  • Your Swagger UI is running on: /explorer
  • Your Swagger definition is running on: /swagger/api
  • Health endpoint: /health

There are two different options for debugging a Flask project:

  1. Run python manage.py runserver to start a native Flask development server. This comes with the Werkzeug stack-trace debugger, which will present runtime failure stack-traces in-browser with the ability to inspect objects at any point in the trace. For more information, see Werkzeug documentation.
  2. Run python manage.py debug to run a Flask development server with debug exposed, but the native debugger/reloader turned off. This grants access for an IDE to attach itself to the process (i.e. in PyCharm, use Run -> Attach to Local Process).

You can also verify the state of your locally running application using the Selenium UI test script included in the scripts directory.

Note for Windows users: gunicorn is not supported on Windows. You may start the server with python manage.py run on your local machine or build and start the Dockerfile.

IBM Cloud Developer Tools

Install IBM Cloud Developer Tools on your machine by running the following command:

curl -sL https://ibm.biz/idt-installer | bash

Create an application on IBM Cloud by running:

ibmcloud dev create

This will create and download a starter application with the necessary files needed for local development and deployment.

Your application will be compiled with Docker containers. To compile and run your app, run:

ibmcloud dev build
ibmcloud dev run

This will launch your application locally. When you are ready to deploy to IBM Cloud on Kubernetes, run one of the commands:

ibmcloud dev deploy -t container // to K8s cluster

You can build and debug your app locally with:

ibmcloud dev build --debug
ibmcloud dev debug

License

This sample application is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 and the Apache License, Version 2.

Apache License FAQ

python-flask-app's People

Contributors

chrisschneideribm avatar christoc-ibm avatar chuckcox avatar dependabot[bot] avatar enelli avatar gee4vee avatar imgbotapp avatar jmeis avatar jruano03 avatar keithkade avatar nfstein avatar reedcozart avatar stevemar avatar youming-lin 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

Watchers

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

python-flask-app's Issues

Adding libraries such as "pandas" fails

when i add pandas into Pipfile deployment as CF app succeeds however the app crashes. when i checked the logs i saw that pandas module could not be found. the pipfile seems to be not working properly

ibm dev build issue, Dockerfile-tools command returns code 2

Hello. This is my first time posting an issue on GitHub.

I am experiencing an issue running "ibmcloud dev build" after following the instructions for logging into my IBM account and setting up ibmcloud CLI as seen here and then following the README here. I am sure I've missed something, but I've tried running through this process on two Ubuntu machines.

My development environment:

  • Ubuntu 18.04, 64-bit

The issue
To get to the point I am stuck at, I ran the following commands:

  • curl -sL https://ibm.biz/idt-installer | bash
  • ibmcloud dev help (verify installation, output is the help page)
  • ibmcloud login (my login is accepted)
  • ibmcloud dev create ... I add a Cloudant service
  • ibmcloud target --cf (I also have to run ibmcloud target -g RESOURCE_GROUP which is already created on the IBM portal)
  • I then run ibmcloud dev build --trace and receive the following output

IBM Cloud CLI version: 0.21.0+f5d1134-2019-12-16T06:43:08+00:00
dev plugin version: 2.4.0
cr plugin version: 0.1.437
ks plugin version: 0.4.66
cf plugin version: 6.46.1+4934877ec.2019-08-23
docker version: 19.03.5, build 633a0ea838
docker-compose version: 1.25.0, build 0a186604
kubectl version: MISSING
helm client version: v2.16.1
git version: 2.17.1

API endpoint: https://cloud.ibm.com
Region: us-south
User: [redacted]
Account: [redacted]
Resource group: Default
CF API endpoint: https://api.us-south.cf.cloud.ibm.com (API version: 2.142.0)
Org: [redacted]
Space: dev

IBM Cloud CLI version: 0.21.0+f5d1134-2019-12-16T06:43:08+00:00
dev plugin version: 2.4.0
cr plugin version: 0.1.437
ks plugin version: 0.4.66
cf plugin version: 6.46.1+4934877ec.2019-08-23
docker version: 19.03.5, build 633a0ea838
docker-compose version: 1.25.0, build 0a186604
kubectl version: MISSING
helm client version: v2.16.1
git version: 2.17.1

API endpoint: https://cloud.ibm.com
Region: us-south
User: [redacted]
Account: [redacted]
Resource group: Default
CF API endpoint: https://api.us-south.cf.cloud.ibm.com (API version: 2.142.0)
Org: [redacted]
Space: dev

Getting service credentials for the application.

... credentials received without issue ...

RESPONSE STATUS: 200

OK
Service credentials file(s) credentials.json, localdev-config.json, server/localdev-config.json already present in .gitignore.
Validating Docker image name
OK
Using these variable values:
01: ContainerName string =
02: ContainerNameRun string = test-flask-run
03: ContainerNameTools string = test-flask-tools
04: HostPathRun string = .
05: HostPathTools string = .
06: ContainerPathRun string = /app
07: ContainerPathTools string = /app
08: IsUseRootUserTools bool = false
09: BuildCmdRun string = python manage.py build
10: BuildCmdDebug string = python manage.py build
11: TestCmd string = python manage.py test
12: DebugCmd string = python manage.py debug
13: RunCmd string =
14: ContainerPortMap string = 3000:3000
15: ContainerPortMapDebug string = 5858:5858
16: ImageNameTools string = test-flask-tools
17: ImageNameRun string = test-flask-run
18: DockerfileRun string = Dockerfile
19: DockerfileTools string = Dockerfile-tools
20: ContainerMountsRun []map[string]string = []
21: ContainerMountsTools []map[string]string = []
22: IsDebug bool = false
23: IsTrace bool = true
24: IsJSON bool = false
25: Version string = 0.0.3
26: DeployTarget string =
27: IsForce bool = false
28: Language string =
29: IbmCluster string =
30: ClusterRole string = cluster-admin
31: ClusterServiceAccount string = kube-system:default
32: ChartPath string = chart/test
33: DockerRegistry string =
34: GeneratedID string = b310ef9b-fc1e-46a8-9357-f6a583fdc5b5
35: CredsFilepath string = credentials.json, localdev-config.json, server/localdev-config.json
36: NoOpen bool = false
37: WebAppRoot string =
38: ContainerShell string = /bin/sh
39: ContainerShellTarget string = tools
40: AppID string = [redacted]
41: Hostname string =
42: Domain string =
43: JobID string =
44: JobExecID string =
45: StageID string =
Checking if Docker container test-flask-tools is running
OK
Checking Docker image history to see if image already exists
OK
Creating image test-flask-tools based on Dockerfile-tools ...
Image will have user [my user] with id 1000 added

Executing docker image build --file Dockerfile-tools --tag test-flask-tools --rm --pull --build-arg bx_dev_userid=1000 --build-arg
bx_dev_user=[my user] .

Waiting for Docker image to build
Sending build context to Docker daemon 4.268MB
Step 1/17 : FROM registry.access.redhat.com/ubi8/python-36
latest: Pulling from ubi8/python-36
Digest: sha256:5ae11a48f1d0716873d209b344638c9c31bfa91f19f20f76dbc173137be4b8f6
Status: Image is up to date for registry.access.redhat.com/ubi8/python-36:latest
---> b21dfe8a3a50
Step 2/17 : WORKDIR /app
---> Using cache
---> d70da69cefd0
Step 3/17 : COPY requirements.txt /tmp/requirements.txt
---> Using cache
---> 5a45b626221b
Step 4/17 : USER root
---> Using cache
---> 0dccc30a4acb
Step 5/17 : RUN yum install python3-pip wget
---> Using cache
---> 1ed2ec8899f8
Step 6/17 : RUN pip install --upgrade pip && pip install --upgrade pipenv && pip install --upgrade -r /tmp/requirements.txt
---> Using cache
---> 3074d677d235
Step 7/17 : ENV FLASK_APP=server/init.py
---> Using cache
---> 735e467ea2b3
Step 8/17 : ENV FLASK_DEBUG=true
---> Using cache
---> 061ac59ce098
Step 9/17 : COPY --chown=1000:1000 . /app
---> Using cache
---> 51aa02c9f9a0
Step 10/17 : COPY --chown=1000:1000 run-dev /bin
---> Using cache
---> d24702ec271a
Step 11/17 : RUN chmod 777 /bin/run-dev
---> Using cache
---> 8353d154409b
Step 12/17 : USER 1000
---> Using cache
---> e0c5de793ceb
Step 13/17 : ARG bx_dev_user=root
---> Using cache
---> 4539a0358953
Step 14/17 : ARG bx_dev_userid=1000
---> Using cache
---> 9396912b3ac7
Step 15/17 : RUN BX_DEV_USER=$bx_dev_user
---> Using cache
---> 67951a4606c1
Step 16/17 : RUN BX_DEV_USERID=$bx_dev_userid
---> Using cache
---> b1f3fb26dc94
Step 17/17 : RUN if [ "$bx_dev_user" != root ]; then adduser -D -s /bin/bash -u $bx_dev_userid $bx_dev_user; fi
---> Running in 6d3c62f1c436
Usage: adduser [options] LOGIN
adduser -D
adduser -D [options]

Options:
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change default useradd configuration
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-g, --gid GROUP name or ID of the primary group of the new
account
-G, --groups GROUPS list of supplementary groups of the new
account
-h, --help display this help message and exit
-k, --skel SKEL_DIR use this alternative skeleton directory
-K, --key KEY=VALUE override /etc/login.defs defaults
-l, --no-log-init do not add the user to the lastlog and
faillog databases
-m, --create-home create the user's home directory
-M, --no-create-home do not create the user's home directory
-N, --no-user-group do not create a group with the same name as
the user
-o, --non-unique allow to create users with duplicate
(non-unique) UID
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as the user
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping

The command '/bin/sh -c if [ "$bx_dev_user" != root ]; then adduser -D -s /bin/bash -u $bx_dev_userid $bx_dev_user; fi' returned a non-zero code: 2
FAILED
An error exit status 2 was encountered while building the Docker image.

exit status 2

I am new to Docker also. I found this issue in the Nodejs IBM microservice repo under Dockerfile-tools: RUN if [ "$bx_dev_user" != root ]; then useradd -ms /bin/bash -u $bx_dev_userid $bx_dev_user; fi

This differs from this Pytohn repo's Dockerfile-tools last line of: RUN if [ "$bx_dev_user" != root ]; then adduser -D -s /bin/bash -u $bx_dev_userid $bx_dev_user; fi

The difference is "-D -s" vs "-ms" after adduser.

If I use the "-ms" option from the Nodejs repo, I get the same output but it differs at the end:

Step 17/17 : RUN if [ "$bx_dev_user" != root ]; then adduser -ms /bin/bash -u $bx_dev_userid $bx_dev_user; fi
---> Running in d91079108274
adduser: Permission denied.
adduser: cannot lock /etc/passwd; try again later.
The command '/bin/sh -c if [ "$bx_dev_user" != root ]; then adduser -ms /bin/bash -u $bx_dev_userid $bx_dev_user; fi' returned a non-zero code: 1
FAILED
An error exit status 1 was encountered while building the Docker image.

exit status 1

Also ...

This may not have any bearing on this docker issue, but I am aware ibmcloud is unable to find "kubectl" even though it is installed. I do not intend to use Kubernetes as a deployment option - is it fine for Docker that "kubectl" is not found?

If anyone could point out what I'm missing or what I could try, that would be great. : ) Thank you!

Cloud foundry manifest file missing

Steps

  1. create a new flask application from starter kit
  2. the app tells the user to get the source code from https://github.com/IBM/python-flask-app The user can download the zip.
  3. the zip is missing the manifest.yaml file, however the deployment toolchain puts it back when it clones the directory to gitlab.

This is very confusing for a new user. I suggest adding the manifest file here for cloud foundry. It is simply ignored if the user decides to deploy to Kubernetes or Code Engine. Happy to create a PR.

@stevemar

ps: I think the file used to be here and maybe removed intentionally recently?

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.