c Python Python Web OS PaaS HerokuWeb Dash+Plotly Web Dash PuLP ORM SQLAlchemy Heroku 1. Python Web Python Web Python 2. IT 2.1 IT IT Web SaaS (Software as a Service) IaaS/HaaS (Infrastruc- 180 0001 3 3 1 wuwei@st.seikei.ac.jp 104 8011 5 3 2 kikuchi-k4@asahi.com ture/hardware as a Service) PaaS (Platform as a Service) 1 PaaS Heroku PaaS Web Operating System (OS) (DB) PaaS Web Heroku Python Python Node.js Go Ruby 2.2 Python Web Python Web Django Bottle Flask Dash Web Dash Flask Web Web Web 1 https://ja.wikipedia.org/wiki/ # 2018 12 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited. 39 777
1 3 [1] 4. Web OS macos High Sierra 10.13.6 Python Python 3.6.5 DB PostgreSQL 10.4 Git 2.17.1 Mac Package Manager Homebrew 1.7.1 2 Python PyPI (Python Package Index) pip 5. 3. [1] 6.8 30 2 3 3 3 1 1. 2. 3. 4. 4 Heroku Web Heroku 5.1 Heroku Git Git Homebrew $ brew install git Heroku Heroku.com 2 Heroku Heroku Command Line Interface (CLI) $ brew install heroku/brew/heroku Heroku CLI Heroku $ heroku login Python Virtualenv $ pip install virtualenv 5.2 nsp app Git Python 2 https://signup.heroku.com/dc 778 40 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited.
2 Python dash 0.22.0 Web Python dash-core-components 0.26.0 Dash UI dash-html-components 0.11.0 Dash HTML dash-renderer 0.13.0 Dash gunicorn 2018.7.1 Unix OS WSGI HTTP plotly 3.1.0 PuLP 1.6.8 psycopg2 2.7.5 PostgreSQL SQLAlchemy 1.2.10 Python SQL ORM (Object-Relational Mapping) virtualenv 16.0.0 $ cd nsp_app $ git init # git $ virtualenv venv # $ source venv/bin/activate # $ deactivate 2 pip install Python requirements.txtpip freeze $ pip freeze > requirements.txt Heroku Heroku Procfile web: gunicorn app: server Procfile Heroku 5.3 Heroku PostgreSQL PostgreSQL PostgreSQL $ brew install postgresql Mac PostgreSQL $ brew services start postgresql PostgreSQL $ brew services stop postgresql nsp $ createdb nsp $ psql -l Python (Object-Relational Mapping, ORM) SQLAlchemy SQLAlchemy Python engine from sqlalchemy import create_engine DB_URL = postgres ://@localhost:5432/nsp engine = create_engine(db_url, echo=true) create engine echo True SQL engine 2018 12 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited. 41 779
3 Nurse Nurse +id: Integer +name: String(16) +email: String(128) from sqlalchemy.orm import scoped_session, sessionmaker session = scoped_session(sessionmaker(bind = engine)) Nurse SQLAlchemy id (name) email Nurse 3 Nurse SQLAlchemy sqlalchemy.org 3 from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String Base = declarative_base() class Nurse(Base): # nurse tablename = nsp_nurse id = Column( id, Integer, primary_key= True) name = Column( name, String(16), nullable=false) email = Column( email, String(128)) Base.metadata.create_all(bind=engine) 4 declarative base() 14 Base.metadata.create all(bind=engine) Base CREATE TABLE Nurse CRUD (Create, Read, Update, Delete) # new_nurse = Nurse(name= Hanako ) session.add(new_nurse) session.commit() # nurses = session.query(nurse).all() nurse_1 = session.query(nurse).filter(nurse. id == 1).one_or_none() nurse_1.email = hanako@orsj.or.jp session.commit() # session.delete(nurse_1) session.commit() s date e date SQLAlchemy 4 5.4 Heroku Heroku Web Web Heroku Heroku $ heroku create heroku create https://.herokuapp.com Heroku $ heroku addons:create heroku-postgresql $ heroku config heroku config URL 5.3 SQLAlchemy URL # DB_URL = postgres ://@localhost:5432/nsp DB_URL = [heroku config URL] 5.5 5.7 Web # 3 http://docs.sqlalchemy.org/en/latest/index.html 4 http://docs.sqlalchemy.org/en/latest/orm/relationsh ips.html 780 42 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited.
$ git add. # git $ git commit -m Initial app nsp # $ git push heroku master # heroku Heroku dyno OS dyno dyno Heroku dyno 1 $ heroku ps:scale web=1 1 Dash Web $ heroku open $ git status # $ git add. # $ git commit -m a memo of the changes # $ git push heroku master logs $ heroku logs --tail 5.5 Dash Web Dash Dash HTML Dash HTML Components (DHC) Dash UI Dash Core Components (DCC)Web HTML UI HTML HTML CSS Javascript UI Web Web Dash Dash 5 import dash import dash_core_components as dcc import dash_html_components as html from datetime import date as Dt app = dash.dash( name ) server = app.server app.layout = html.div([ html.h2( ), dcc.datepickerrange( id= date-range -picker, ]) start_date=dt(2017,7,1), end_ date=dt(2017,7,21) ), html.div(id= display-date ) if name == main : app.run_server(debug=true) layout DHC DCC 1 Dash Dash id id callback 5 https://dash.plot.ly/ 2018 12 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited. 43 781
date-range-picker start date end date display-date callback from dash.dependencies import Output, Input @app.callback(output( display-date, children ), [Input( date-range -picker, start_date ), Input( date-range -picker, end_date )]) def show_schedule(s_date, e_date): return +s_date+ + e_date 1 2017-07-01 2017-07-21 UI 5.6 PuLP [1] 3 (NSP) M N K R G r r a rjk,b rjk r j k x ijk i j k 1 0 0-1 α rjk,α+ rjk r j k x =(x ijk ) α =(α rjk ), α+ =(α + rjk ) a rjk α rjk x ijk b rjk + α + rjk, i G r r R, j N, k K, (1) x ijk {0, 1}, i M, j N, k K, (2) α rjk,α + rjk 0, r R, j N, k K. (3) minimize (w rjk α rjk + w+ rjk α+ rjk ) (4) r R j N k K w rjk w+ rjk r j k α rjk α+ rjk NSP PuLP PuLP [2] 11 ins NSP x import pulp from datetime import timedelta def solve_nsp(ins): day_diff = ins.e_date - ins.s_date # model = pulp.lpproblem("nsp", pulp. LpMinimize) # x suf = [(i,ins.s_date+j,k) for i in ins. nur_set for j in range(day_diff+1) for k in ins.sft_set] x = pulp.lpvariable.dicts("x", suf, cat= pulp.lpbinary) α α + Python alpha m alpha p PuLP LpProblem += # 4 model += sum(alpha_p[grp_id,day,sft] for grp_id,day,sft in alpha_p) + sum(alpha_m[ grp_id,day,sft] for grp_id,day,sft in alpha_m), "Obj" # 1 for req in ins.grp_reqs: model += sum(x[nur,req.date,req.shift] for nur in grp_nurs[req.grp_id]) >= req.lb - alpha_m[req.grp_id,req.date, req.shift], "Con_lb{:}{:}{:}".format( req.grp_id,req.date,req.shift) PuLP PuLP 782 44 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited.
2 Plotly CBC (COIN Branch and Cut) LpProblem solve solver = pulp.solvers.pulp_cbc_cmd() status = model.solve(solver) solve 2 status value() 5.7 Plotly Dash Plotly NSP Plotly Matplotlib Plotly 6 Annotated Heatmap Annotated Heatmap plot.ly 7 5.6 x text 1 list y text1 list z val 2 list z text 2 list colorscale fig.layout.margin.update({"r": 0}) fig.layout.xaxis.update({"ticks":, " showgrid":false}) fig.layout.yaxis.update({"ticks":, " showgrid":false}) 2 4 fig 5.5 DCC Graph dcc.graph(id="schedule", figure=fig) 2 6. Python PaaS Heroku Python Web [1] 2018. [2] Python 2016. fig = ff.create_annotated_heatmap(z_val, x= x_text, y=y_text, annotation_text=z_text, colorscale=colorscale, font_colors=[ black ], hoverinfo= none, xgap=2, ygap =2) 6 https://images.plot.ly/plotly-documentation/imag es/python cheat sheet.pdf 7 https://plot.ly/python/annotated heatmap/ 2018 12 Copyright c by ORSJ. Unauthorized reproduction of this article is prohibited. 45 783