Skip to content

Update docker-compose.yaml to mount init.sql file for PostgreSQL cont… #238

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

Merged
Merged
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
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ services:
ports:
- "5432:5432"
volumes:
- ./data/postgres:/var/lib/postgresql/data
# - ./data/postgres:/var/lib/postgresql/data
- ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
pull_policy: always
deploy:
resources:
Expand Down
14 changes: 14 additions & 0 deletions docker/postgres/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE USER server WITH PASSWORD 'password-server';

CREATE DATABASE server_db;

\c server_db

CREATE TABLE notebooks (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
path VARCHAR(100) NOT NULL
);

GRANT ALL PRIVILEGES ON TABLE notebooks TO server;
GRANT ALL PRIVILEGES ON SEQUENCE notebooks_id_seq TO server;
33 changes: 0 additions & 33 deletions examples/notebook_20240625163616.ipynb

This file was deleted.

4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Flask==3.0.3
Flask-Cors==4.0.1
requests==2.32.2
requests==2.32.2
Flask-SQLAlchemy==3.1.1
psycopg2-binary==2.9.9
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions server/app/models/notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask import jsonify
from database import db

class NotebookModel(db.Model):

__tablename__ = 'notebooks'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String, nullable=False)
path = db.Column(db.String, nullable=False)

def __init__(self, name, path):
self.name = name
self.path = path

def to_dict(self):
return {
'id': self.id,
'name': self.name,
'path': self.path
}



File renamed without changes.
6 changes: 5 additions & 1 deletion server/routes/notebook.py → server/app/routes/notebook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import Blueprint, jsonify, request
from models.notebook import Notebook
from app.services.notebook import Notebook

notebook_blueprint = Blueprint('notebook', __name__)

Expand All @@ -11,6 +11,10 @@ def notebook():
}
)

@notebook_blueprint.route('/notebook/all', methods=['GET'])
def get_all_notebooks():
return Notebook.get_all_notebooks()

@notebook_blueprint.route('/notebook/create', methods=['POST'])
def create_notebook():
data = request.get_json()
Expand Down
Empty file added server/app/services/__init__.py
Empty file.
32 changes: 25 additions & 7 deletions server/models/notebook.py → server/app/services/notebook.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from flask import jsonify
from app.models.notebook import NotebookModel
from datetime import datetime
import requests
import uuid
from database import db
import json
import os

class Notebook(object):
class Notebook:

def __init__(self) -> None:
self.jupyter_server_path = os.environ.get("JUPYTER_SERVER_PATH", "http://localhost:8888")
@staticmethod
def get_all_notebooks():
notebooks = NotebookModel.query.all()

# Convert the notebooks to dictionaries
notebooks_dict = [notebook.to_dict() for notebook in notebooks]

# Now you can serialize notebooks_dict
notebooks_json = json.dumps(notebooks_dict)

return notebooks_json

@staticmethod
def create_notebook(notebook_name: str = None) -> None:
Expand Down Expand Up @@ -75,8 +85,7 @@ def create_notebook_with_init_cells(notebook_name: str = None) -> None:
},
"language_info": {
"name": 'python'
},
"uuid": str(uuid.uuid4())
}
},
"nbformat": 4,
"nbformat_minor": 4
Expand All @@ -88,5 +97,14 @@ def create_notebook_with_init_cells(notebook_name: str = None) -> None:
json=data
)

notebook = NotebookModel(
name=notebook_name,
path=f'work/{notebook_name}'
)

db.session.add(notebook)
db.session.commit()

return response.json()


3 changes: 3 additions & 0 deletions server/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()
22 changes: 16 additions & 6 deletions server/run.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
from flask import Flask
from flask_cors import CORS
from routes.notebook import notebook_blueprint
from database import db
from app.routes.notebook import notebook_blueprint

app = Flask(__name__)

allowed_origins = ["http://localhost:5001", "http://localhost:3000"]

CORS(app, resources={
r"/*": {"origins": allowed_origins}
})

def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://server:password-server@localhost:5432/server_db'
db.init_app(app)

allowed_origins = ["http://localhost:5001", "http://localhost:3000"]
CORS(app, resources={
r"/*": {"origins": allowed_origins}
})

return app

app = create_app()

app.register_blueprint(notebook_blueprint)

Expand Down
1 change: 0 additions & 1 deletion webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const App = () => {
const handleNewNotebookClick = () => {
if (handleUnsavedChanges()) {
NotebookModel.createNotebook(`${baseUrl}work`).then((data) => {
console.log('Created new notebook:', data);
const notebookPath = `${baseUrl}${data.path}`
NotebookModel.fetchNotebook(notebookPath).then((data) => {
setNotebook(data);
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/notebook/content/Runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function Runs() {
const [data, setData] = useState(null);

useEffect(() => {
fetch('http://localhost:5002/notebook')
fetch('http://localhost:5002/notebook/all')
.then(response => response.json())
.then(data => setData(data))
.catch(error => console.error('Error:', error));
Expand Down
Loading