Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update repo #25

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .do/app.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions .do/deploy.template.yaml

This file was deleted.

48 changes: 48 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Virtual environment
venv/
google_key.json
.env
# Compiled Python files
__pycache__/
*.pyc
*.pyo
*.pyd

# Jupyter Notebook files
.ipynb_checkpoints

# Environment variables
.env

# Logs
logs/
*.log

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover

# PyBuilder
target/

# Pyre type checker
.pyre/

# VS Code settings and cache
.vscode/
*.code-workspace

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyright
.pyrightcache/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/PlanetIX-Python-service.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# official Python base image
FROM python:3.9-slim

# working directory
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code into the container
COPY . .

# Expose the port the app will run on
EXPOSE 8000

# Set environment variables
ENV GOOGLE_APPLICATION_CREDENTIALS /app/path/to/your-gcp-credentials.json
ENV MYSQL_HOST your_mysql_host
ENV MYSQL_USER your_mysql_user
ENV MYSQL_PASSWORD your_mysql_password
ENV MYSQL_DATABASE your_database_name

# Start the application using Gunicorn
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "main:app"]
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: python server.py
web: gunicorn main:app
92 changes: 57 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@
## Getting Started
# PlanetIX PY service

We provide a sample app using Python that you can deploy on App Platform. These steps will get this sample application running for you using App Platform.
This project is a Python Flask application hosted on Digital Ocean App Platform that fetches data from Google BigQuery and saves it to a MySQL server every two hours.

**Note: Following these steps may result in charges for the use of DigitalOcean services.**
## Prerequisites

### Requirements
- Python 3.9 or higher
- A Google Cloud account with a valid service account JSON key file
- A MySQL server with the appropriate credentials

* You need a DigitalOcean account. If you don't already have one, you can sign up at https://cloud.digitalocean.com/registrations/new.
## Setup

## Deploying the App
### 1. Clone the repository

Click this button to deploy the app to the DigitalOcean App Platform. If you are not logged in, you will be prompted to log in with your DigitalOcean account.
Clone the project repository from GitHub to your local machine:

[![Deploy to DigitalOcean](https://www.deploytodo.com/do-btn-blue.svg)](https://cloud.digitalocean.com/apps/new?repo=https://github.com/digitalocean/sample-python/tree/main)
```bash
git clone https://github.com/myusername/my-python-app.git
```
Create and activate a virtual environment

Using this button disables the ability to automatically re-deploy your app when pushing to a branch or tag in your repository as you are using this repo directly.
### 2. Navigate to the project directory and create a virtual environment:

If you want to automatically re-deploy your app, [fork](https://docs.github.com/en/github/getting-started-with-github/fork-a-repo) the GitHub repository to your account so that you have a copy of it stored to the cloud. Click the **Fork** button in the GitHub repository and follow the on-screen instructions.
```
cd my-python-app
python -m venv venv
```
Activate the virtual environment:

After forking the repo, you should now be viewing this README in your own GitHub org (e.g. `https://github.com/<your-org>/sample-python`). To deploy the new repo, visit https://cloud.digitalocean.com/apps and click **Create App**. Then, click **GitHub**, select the repository you created and select the `main` branch. App Platform will inspect the code, automatically detect the kind of component to create, and use the correct buildpack to create and deploy a container.
On Unix-based systems (Linux/macOS):

After clicking the **Deploy to DigitalOcean** button or completing the instructions above to fork the repo, follow these steps:
```source venv/bin/activate```

1. Configure the app such as specifying HTTP routes, environment variables or adding a database.
1. Provide a name for your app and select which region you want to deploy your app to and click **Next**. The closest region to you should be selected by default. All App Platform apps are routed through a global CDN. So this will not affect your app performance, unless it needs to talk to external services.
1. On the following screen, leave all the fields as they are and click **Next**.
1. Confirm your **Plan** settings and how many containers you want to launch and click **Launch Basic/Pro App**.
1. You should see a "Building..." progress indicator. You can click **View Logs** to see more details of the build.
1. It can take a few minutes for the build to finish, but you can follow the progress in the **Deployments** tab.
1. Once the build completes successfully, click the **Live App** link in the header and you should see your running application in a new tab, displaying the home page.
On Windows:

### Making Changes to Your App
```venv\Scripts\activate.bat```
### 3. Install the dependencies
Install the required dependencies using the requirements.txt file:
```pip install -r requirements.txt```

If you followed the steps to fork the repo and used your own copy when deploying the app, you can push changes to your fork and see App Platform automatically re-deploy the update to your app. During these automatic deployments, your application will never pause or stop serving request because App Platform offers zero-downtime deployments.
### 4.Set up the environment variables
Create a .env file in the project root directory and populate it with the necessary environment variables:

Here's an example code change you can make for this app:
```
GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-gcp-credentials.json
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=your_mysql_password
MYSQL_DATABASE=your_database_name
```

1. Edit `server.py` and replace "Hello!" on line 12 with a different greeting
1. Commit the change to the `main` branch. Normally it's a better practice to create a new branch for your change and then merge that branch to `main` after review, but for this demo you can commit to the `main` branch directly.
1. Visit https://cloud.digitalocean.com/apps and navigate to your sample app.
1. You should see a "Building..." progress indicator, just like when you first created the app.
1. Once the build completes successfully, click the **Live App** link in the header and you should see your updated application running. You may need to force refresh the page in your browser (e.g. using **Shift+Reload**).
## Running the project locally
### Running the Flask application
Run the Flask application with the following command:

### Learn More
```python main.py```

You can learn more about the App Platform and how to manage and update your application at https://www.digitalocean.com/docs/app-platform/.
The Flask application will start running, and you can access it at http://localhost:5000.

## Deleting the App

When you no longer need this sample application running live, you can delete it by following these steps:
1. Visit the Apps control panel at https://cloud.digitalocean.com/apps.
2. Navigate to the sample app.
3. In the **Settings** tab, click **Destroy**.
## Running the project with Docker
Build the Docker images
Build the Docker images for the Flask application and the script:

**Note: If you do not delete your app, charges for using DigitalOcean services will continue to accrue.**

```
docker build -t my-python-app
docker build -t my-python-app-script -f Dockerfile.script .
```
Run the Docker containers
Run the Docker containers for the Flask application and the script:

```
docker run -p 8000:8000 --name my-python-app-instance my-python-app
docker run --name my-python-app-script-instance my-python-app-script
```

The Flask application will be accessible at http://localhost:8000.
Loading