aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitsuo Tokumori <[email protected]>2025-03-08 16:03:30 +0900
committerMitsuo Tokumori <[email protected]>2025-03-08 16:03:30 +0900
commit51163b167cce01af6101438e5e61145ad798f213 (patch)
tree9c8e75266cedfb205db175b0b2bc41b49df75cea
parentd9af103b9a8aed86d6ac834f1240edfb2173ffa0 (diff)
downloadmasu-51163b167cce01af6101438e5e61145ad798f213.tar.gz
masu-51163b167cce01af6101438e5e61145ad798f213.tar.bz2
masu-51163b167cce01af6101438e5e61145ad798f213.zip
Restructure python code to be modular
The python code is now a package named app. app/models: db models app/routes: flask blueprints app/static: css, js app/templates: jinja html templates
-rw-r--r--.gitignore1
-rw-r--r--README25
-rw-r--r--app/__init__.py8
-rw-r--r--app/config.def.py (renamed from config.def.py)0
-rw-r--r--app/routes/main.py (renamed from app.py)13
-rw-r--r--app/static/block_time.js (renamed from static/block_time.js)0
-rw-r--r--app/static/block_weather.js (renamed from static/block_weather.js)0
-rw-r--r--app/static/script.js (renamed from static/script.js)0
-rw-r--r--app/static/style.css (renamed from static/style.css)0
-rw-r--r--app/templates/index.html (renamed from templates/index.html)0
-rw-r--r--app/utils/block_weather.py (renamed from block_weather.py)9
-rw-r--r--requirements.txt18
-rw-r--r--run.py5
13 files changed, 54 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore
index a58cf92..65bce24 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
.venv/
__pycache__/
tmp/
+instance/
config.py
diff --git a/README b/README
index 71c0ae3..e4f195b 100644
--- a/README
+++ b/README
@@ -1,34 +1,29 @@
MASU - minimalist dashboard
-what: grid of blocks (clocks, weather, etc) w/ real-time updates
-
-how: flask + vanilla js, no bloat
-
-why: simple, tweakable, fast
+A web based grid of blocks (e.g., clocks, weather) with "real-time"
+updates. Implemented in flask and vanilla js (trying to minimize bloat).
+Features: simple, tweakable, fast.
setup:
git clone <url>
python -m venv .venv
source .venv/bin/activate
- pip install flask requests
- flask run
+ pip install -r requirements.txt
+ python run.py
use:
http://localhost:5000
reload/tweak in header
-files:
-
- app.py: server
- static/: css, js
- templates/: html
-
hack:
- add blocks: app.py + script.js
- set intervals: script.js
+ Each block is defined by:
+ * html: front-end elements (modify: app/templates/index.html) (for now)
+ * css: front-end design (modify: app/static/style.css)
+ * js: front-end code (new file: app/static/block_name.js)
+ * python: back-end code (app/routes/main.py, app/utils/block_name.py)
deploy:
diff --git a/app/__init__.py b/app/__init__.py
new file mode 100644
index 0000000..4bf2b5a
--- /dev/null
+++ b/app/__init__.py
@@ -0,0 +1,8 @@
+from flask import Flask
+from .routes import main
+
+def create_app():
+ app = Flask(__name__)
+ app.config.from_pyfile('config.py')
+ app.register_blueprint(routes.main.bp)
+ return app
diff --git a/config.def.py b/app/config.def.py
index c80065d..c80065d 100644
--- a/config.def.py
+++ b/app/config.def.py
diff --git a/app.py b/app/routes/main.py
index e7a6075..842a09b 100644
--- a/app.py
+++ b/app/routes/main.py
@@ -4,16 +4,18 @@ import datetime
import zoneinfo
import locale
-import block_weather
+from ..utils import block_weather
locale.setlocale(locale.LC_ALL, '')
-app = flask.Flask(__name__)
+bp = flask.Blueprint('main', __name__)
+
def index():
return flask.render_template("index.html")
+
def get_time():
# TODO: date should be passed as JSON
date = datetime.datetime.now(zoneinfo.ZoneInfo("Asia/Tokyo"))
@@ -33,7 +35,8 @@ def get_time():
]
return flask.jsonify({"time": "\n".join(lines)})
[email protected]("/weather")
+
[email protected]("/weather")
def get_weather():
city = flask.request.args.get("city", "Kofu, JP")
data = block_weather.get_current_weather(city)
diff --git a/static/block_time.js b/app/static/block_time.js
index d6510ae..d6510ae 100644
--- a/static/block_time.js
+++ b/app/static/block_time.js
diff --git a/static/block_weather.js b/app/static/block_weather.js
index 4ae6d2d..4ae6d2d 100644
--- a/static/block_weather.js
+++ b/app/static/block_weather.js
diff --git a/static/script.js b/app/static/script.js
index efd6836..efd6836 100644
--- a/static/script.js
+++ b/app/static/script.js
diff --git a/static/style.css b/app/static/style.css
index a186f24..a186f24 100644
--- a/static/style.css
+++ b/app/static/style.css
diff --git a/templates/index.html b/app/templates/index.html
index 332b78b..332b78b 100644
--- a/templates/index.html
+++ b/app/templates/index.html
diff --git a/block_weather.py b/app/utils/block_weather.py
index 84c0092..eec4e4a 100644
--- a/block_weather.py
+++ b/app/utils/block_weather.py
@@ -7,17 +7,16 @@ Providers:
https://openweathermap.org/price
"""
+import flask
import requests
import pandas as pd
-import config
-
def get_current_weather(city: str) -> dict:
"""https://openweathermap.org/current"""
pos = _get_position(city)
if not pos:
return None
- url = f"http://api.openweathermap.org/data/2.5/weather?lat={pos['lat']}&lon={pos['lon']}&appid={config.OPENWEATHER_API_KEY}&units=metric"
+ url = f"http://api.openweathermap.org/data/2.5/weather?lat={pos['lat']}&lon={pos['lon']}&appid={flask.current_app.config['OPENWEATHER_API_KEY']}&units=metric"
data = requests.get(url).json()
data['summary'] = _format_current_weather(data)
@@ -44,7 +43,7 @@ def get_forecast(city: str) -> dict:
"""https://openweathermap.org/forecast5"""
# 5 days, 3 hours forecast data
lat, lon = _get_position(city)
- url = f"http://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={config.OPENWEATHER_API_KEY}&units=metric"
+ url = f"http://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={flask.current_app.config['OPENWEATHER_API_KEY']}&units=metric"
data = requests.get(url).json()
return data
@@ -56,7 +55,7 @@ def _get_position(name) -> dict:
https://openweathermap.org/api/geocoding-api#direct
"""
limit = 5
- url = f"http://api.openweathermap.org/geo/1.0/direct?q={name}&limit={limit}&appid={config.OPENWEATHER_API_KEY}"
+ url = f"http://api.openweathermap.org/geo/1.0/direct?q={name}&limit={limit}&appid={flask.current_app.config['OPENWEATHER_API_KEY']}"
data = requests.get(url).json()
if not data:
return None
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..52b87df
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,18 @@
+blinker==1.9.0
+certifi==2025.1.31
+charset-normalizer==3.4.1
+click==8.1.8
+Flask==3.1.0
+idna==3.10
+itsdangerous==2.2.0
+Jinja2==3.1.5
+MarkupSafe==3.0.2
+numpy==2.2.3
+pandas==2.2.3
+python-dateutil==2.9.0.post0
+pytz==2025.1
+requests==2.32.3
+six==1.17.0
+tzdata==2025.1
+urllib3==2.3.0
+Werkzeug==3.1.3
diff --git a/run.py b/run.py
new file mode 100644
index 0000000..f6fe47e
--- /dev/null
+++ b/run.py
@@ -0,0 +1,5 @@
+from app import create_app
+
+app = create_app()
+if __name__ == '__main__':
+ app.run()