diff options
Diffstat (limited to 'reveal.js/sqlalchemy.html')
| -rw-r--r--[-rwxr-xr-x] | reveal.js/sqlalchemy.html | 285 |
1 files changed, 142 insertions, 143 deletions
diff --git a/reveal.js/sqlalchemy.html b/reveal.js/sqlalchemy.html index a0d6617..bbcd1ca 100755..100644 --- a/reveal.js/sqlalchemy.html +++ b/reveal.js/sqlalchemy.html | |||
| @@ -98,29 +98,29 @@ | |||
| 98 | metadata = MetaData() | 98 | metadata = MetaData() |
| 99 | 99 | ||
| 100 | production_types_table = Table( | 100 | production_types_table = Table( |
| 101 | "production_types", | 101 | "production_types", |
| 102 | metadata, | 102 | metadata, |
| 103 | Column("production_type_id", Integer, primary_key=True), | 103 | Column("production_type_id", Integer, primary_key=True), |
| 104 | Column("code", String(3), nullable=False, unique=True), | 104 | Column("code", String(3), nullable=False, unique=True), |
| 105 | Column("description", String), # Column("name", String(128)) is possible | 105 | Column("description", String), # Column("name", String(128)) is possible |
| 106 | ) | 106 | ) |
| 107 | 107 | ||
| 108 | bidding_areas_table = Table( | 108 | bidding_areas_table = Table( |
| 109 | "bidding_areas", | 109 | "bidding_areas", |
| 110 | metadata, | 110 | metadata, |
| 111 | Column("bidding_area_id", Integer, primary_key=True), | 111 | Column("bidding_area_id", Integer, primary_key=True), |
| 112 | Column("code", String(3), nullable=False, unique=True), | 112 | Column("code", String(3), nullable=False, unique=True), |
| 113 | Column("name", String(32)), | 113 | Column("name", String(32)), |
| 114 | ) | 114 | ) |
| 115 | 115 | ||
| 116 | production_plans_table = Table( | 116 | production_plans_table = Table( |
| 117 | "production_plans", | 117 | "production_plans", |
| 118 | metadata, | 118 | metadata, |
| 119 | Column("record_created_time", DateTime(timezone=False), primary_key=True), | 119 | Column("record_created_time", DateTime(timezone=False), primary_key=True), |
| 120 | Column("start_time", DateTime(timezone=False), primary_key=True), | 120 | Column("start_time", DateTime(timezone=False), primary_key=True), |
| 121 | Column("bidding_area_id", Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True), | 121 | Column("bidding_area_id", Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True), |
| 122 | Column("production_type_id", Integer, ForeignKey("production_types.production_type_id"), primary_key=True), | 122 | Column("production_type_id", Integer, ForeignKey("production_types.production_type_id"), primary_key=True), |
| 123 | Column("value", Numeric, nullable=False), | 123 | Column("value", Numeric, nullable=False), |
| 124 | ) | 124 | ) |
| 125 | 125 | ||
| 126 | metadata.create_all(engine) # creates the tables | 126 | metadata.create_all(engine) # creates the tables |
| @@ -150,13 +150,13 @@ | |||
| 150 | insert_stmt.execute(bidding_area_id=1, code="NO1", name="Elspot NO1") # insert a single entry | 150 | insert_stmt.execute(bidding_area_id=1, code="NO1", name="Elspot NO1") # insert a single entry |
| 151 | # ... or a list of entries | 151 | # ... or a list of entries |
| 152 | insert_stmt.execute( | 152 | insert_stmt.execute( |
| 153 | [ | 153 | [ |
| 154 | {"bidding_area_id": 2, "code": "NO2", "name": "Elspot NO2"}, | 154 | {"bidding_area_id": 2, "code": "NO2", "name": "Elspot NO2"}, |
| 155 | {"bidding_area_id": 3, "code": "NO3", "name": "Elspot NO3"}, | 155 | {"bidding_area_id": 3, "code": "NO3", "name": "Elspot NO3"}, |
| 156 | {"bidding_area_id": 4, "code": "NO4", "name": "Elspot NO4"}, | 156 | {"bidding_area_id": 4, "code": "NO4", "name": "Elspot NO4"}, |
| 157 | {"bidding_area_id": 5, "code": "NO5", "name": "Elspot NO5"}, | 157 | {"bidding_area_id": 5, "code": "NO5", "name": "Elspot NO5"}, |
| 158 | {"bidding_area_id": 6, "code": "NO6", "name": "Elspot NO6"}, | 158 | {"bidding_area_id": 6, "code": "NO6", "name": "Elspot NO6"}, |
| 159 | ] | 159 | ] |
| 160 | ) | 160 | ) |
| 161 | 161 | ||
| 162 | metadata.bind = engine # no need to explicitly bind the engine from now on | 162 | metadata.bind = engine # no need to explicitly bind the engine from now on |
| @@ -182,21 +182,21 @@ | |||
| 182 | from sqlalchemy.orm import mapper | 182 | from sqlalchemy.orm import mapper |
| 183 | 183 | ||
| 184 | class ProductionType: | 184 | class ProductionType: |
| 185 | def __init__(self, code, description): | 185 | def __init__(self, code, description): |
| 186 | self.code = code | 186 | self.code = code |
| 187 | self.description = description | 187 | self.description = description |
| 188 | 188 | ||
| 189 | def __str__(self): | 189 | def __str__(self): |
| 190 | return self.code | 190 | return self.code |
| 191 | 191 | ||
| 192 | 192 | ||
| 193 | class BiddingArea: | 193 | class BiddingArea: |
| 194 | def __init__(self, code, name): | 194 | def __init__(self, code, name): |
| 195 | self.code = code | 195 | self.code = code |
| 196 | self.name = name | 196 | self.name = name |
| 197 | 197 | ||
| 198 | def __str__(self): | 198 | def __str__(self): |
| 199 | return self.code | 199 | return self.code |
| 200 | 200 | ||
| 201 | mapper(ProductionType, production_types_table) | 201 | mapper(ProductionType, production_types_table) |
| 202 | mapper(BiddingArea, bidding_areas_table) | 202 | mapper(BiddingArea, bidding_areas_table) |
| @@ -212,27 +212,27 @@ | |||
| 212 | from sqlalchemy.orm import relationship | 212 | from sqlalchemy.orm import relationship |
| 213 | 213 | ||
| 214 | class ProductionPlan: | 214 | class ProductionPlan: |
| 215 | def __init__(self, record_created_time, start_time, production_type, bidding_area, value): | 215 | def __init__(self, record_created_time, start_time, production_type, bidding_area, value): |
| 216 | self.record_created_time = record_created_time | 216 | self.record_created_time = record_created_time |
| 217 | self.start_time = start_time | 217 | self.start_time = start_time |
| 218 | self.production_type = production_type | 218 | self.production_type = production_type |
| 219 | self.bidding_area = bidding_area | 219 | self.bidding_area = bidding_area |
| 220 | self.value = value | 220 | self.value = value |
| 221 | 221 | ||
| 222 | def __str__(self): | 222 | def __str__(self): |
| 223 | return ( | 223 | return ( |
| 224 | f"{self.record_created_time} {self.start_time} " | 224 | f"{self.record_created_time} {self.start_time} " |
| 225 | f"{self.production_type} {self.bidding_area} {self.value}" | 225 | f"{self.production_type} {self.bidding_area} {self.value}" |
| 226 | ) | 226 | ) |
| 227 | 227 | ||
| 228 | 228 | ||
| 229 | mapper( | 229 | mapper( |
| 230 | ProductionPlan, | 230 | ProductionPlan, |
| 231 | production_plans_table, | 231 | production_plans_table, |
| 232 | properties = { | 232 | properties = { |
| 233 | "production_type": relationship(ProductionType, backref="production_plans"), | 233 | "production_type": relationship(ProductionType, backref="production_plans"), |
| 234 | "bidding_area": relationship(BiddingArea, backref="production_plans"), | 234 | "bidding_area": relationship(BiddingArea, backref="production_plans"), |
| 235 | }, | 235 | }, |
| 236 | ) | 236 | ) |
| 237 | </code> | 237 | </code> |
| 238 | </pre> | 238 | </pre> |
| @@ -249,32 +249,32 @@ | |||
| 249 | Base = declarative_base() | 249 | Base = declarative_base() |
| 250 | 250 | ||
| 251 | class ProductionType(Base): | 251 | class ProductionType(Base): |
| 252 | __tablename__ = "production_types" | 252 | __tablename__ = "production_types" |
| 253 | 253 | ||
| 254 | production_type_id = Column(Integer, primary_key=True) | 254 | production_type_id = Column(Integer, primary_key=True) |
| 255 | code = Column(String(3), nullable=False, unique=True) | 255 | code = Column(String(3), nullable=False, unique=True) |
| 256 | description = Column(String) | 256 | description = Column(String) |
| 257 | 257 | ||
| 258 | def __init__(self, code, description): | 258 | def __init__(self, code, description): |
| 259 | self.code = code | 259 | self.code = code |
| 260 | self.description = description | 260 | self.description = description |
| 261 | 261 | ||
| 262 | def __str__(self): | 262 | def __str__(self): |
| 263 | return self.code | 263 | return self.code |
| 264 | 264 | ||
| 265 | class BiddingArea(Base): | 265 | class BiddingArea(Base): |
| 266 | __tablename__ = "bidding_areas" | 266 | __tablename__ = "bidding_areas" |
| 267 | 267 | ||
| 268 | bidding_area_id = Column(Integer, primary_key=True) | 268 | bidding_area_id = Column(Integer, primary_key=True) |
| 269 | code = Column(String(3), nullable=False, unique=True) | 269 | code = Column(String(3), nullable=False, unique=True) |
| 270 | name = Column(String(32)) | 270 | name = Column(String(32)) |
| 271 | 271 | ||
| 272 | def __init__(self, code, name): | 272 | def __init__(self, code, name): |
| 273 | self.code = code | 273 | self.code = code |
| 274 | self.name = name | 274 | self.name = name |
| 275 | 275 | ||
| 276 | def __str__(self): | 276 | def __str__(self): |
| 277 | return self.code | 277 | return self.code |
| 278 | </code> | 278 | </code> |
| 279 | </pre> | 279 | </pre> |
| 280 | </section> | 280 | </section> |
| @@ -288,31 +288,31 @@ | |||
| 288 | from sqlalchemy.orm import relationship, backref | 288 | from sqlalchemy.orm import relationship, backref |
| 289 | 289 | ||
| 290 | class ProductionPlan(Base): | 290 | class ProductionPlan(Base): |
| 291 | __tablename__ = "production_plans" | 291 | __tablename__ = "production_plans" |
| 292 | 292 | ||
| 293 | record_created_time = Column(DateTime(timezone=False), primary_key=True) | 293 | record_created_time = Column(DateTime(timezone=False), primary_key=True) |
| 294 | start_time = Column(DateTime(timezone=False), primary_key=True) | 294 | start_time = Column(DateTime(timezone=False), primary_key=True) |
| 295 | bidding_area_id = Column(Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True) | 295 | bidding_area_id = Column(Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True) |
| 296 | production_type_id = Column(Integer, ForeignKey("production_types.production_type_id"), primary_key=True) | 296 | production_type_id = Column(Integer, ForeignKey("production_types.production_type_id"), primary_key=True) |
| 297 | value = Column(Numeric, nullable=False) | 297 | value = Column(Numeric, nullable=False) |
| 298 | 298 | ||
| 299 | # defining relationships. | 299 | # defining relationships. |
| 300 | # the defined attributes will reference 'ProductionType' and 'BiddingArea' objects | 300 | # the defined attributes will reference 'ProductionType' and 'BiddingArea' objects |
| 301 | production_type = relationship(ProductionType, backref=backref("production_plans")) | 301 | production_type = relationship(ProductionType, backref=backref("production_plans")) |
| 302 | bidding_area = relationship(BiddingArea, backref=backref("production_plans")) | 302 | bidding_area = relationship(BiddingArea, backref=backref("production_plans")) |
| 303 | 303 | ||
| 304 | def __init__(self, record_created_time, start_time, production_type, bidding_area, value): | 304 | def __init__(self, record_created_time, start_time, production_type, bidding_area, value): |
| 305 | self.record_created_time = record_created_time | 305 | self.record_created_time = record_created_time |
| 306 | self.start_time = start_time | 306 | self.start_time = start_time |
| 307 | self.production_type = production_type # a 'ProductionType' object | 307 | self.production_type = production_type # a 'ProductionType' object |
| 308 | self.bidding_area = bidding_area # a 'BiddingArea' object | 308 | self.bidding_area = bidding_area # a 'BiddingArea' object |
| 309 | self.value = value | 309 | self.value = value |
| 310 | 310 | ||
| 311 | def __str__(self): | 311 | def __str__(self): |
| 312 | return ( | 312 | return ( |
| 313 | f"{self.record_created_time} {self.start_time} " | 313 | f"{self.record_created_time} {self.start_time} " |
| 314 | f"{self.production_type} {self.bidding_area} {self.value}" | 314 | f"{self.production_type} {self.bidding_area} {self.value}" |
| 315 | ) | 315 | ) |
| 316 | 316 | ||
| 317 | Base.metadata.create_all(engine) # create tables | 317 | Base.metadata.create_all(engine) # create tables |
| 318 | </code> | 318 | </code> |
| @@ -337,27 +337,27 @@ | |||
| 337 | session.add(bidding_area1) | 337 | session.add(bidding_area1) |
| 338 | 338 | ||
| 339 | session.add_all( | 339 | session.add_all( |
| 340 | [ | 340 | [ |
| 341 | BiddingArea("NO2", "Elspot NO2"), | 341 | BiddingArea("NO2", "Elspot NO2"), |
| 342 | BiddingArea("NO3", "Elspot NO3"), | 342 | BiddingArea("NO3", "Elspot NO3"), |
| 343 | BiddingArea("NO4", "Elspot NO4"), | 343 | BiddingArea("NO4", "Elspot NO4"), |
| 344 | BiddingArea("NO5", "Elspot NO5"), | 344 | BiddingArea("NO5", "Elspot NO5"), |
| 345 | ] | 345 | ] |
| 346 | ) | 346 | ) |
| 347 | 347 | ||
| 348 | production_type_B37 = ProductionType("B37", "Thermal unspecified") | 348 | production_type_B37 = ProductionType("B37", "Thermal unspecified") |
| 349 | production_type_B30 = ProductionType("B30", "Wind unspecified") | 349 | production_type_B30 = ProductionType("B30", "Wind unspecified") |
| 350 | 350 | ||
| 351 | session.add_all( | 351 | session.add_all( |
| 352 | [ | 352 | [ |
| 353 | ProductionType("B19", "Wind Onshore"), | 353 | ProductionType("B19", "Wind Onshore"), |
| 354 | ProductionType("B10", "Hydro-electric pure pumped storage head installation"), | 354 | ProductionType("B10", "Hydro-electric pure pumped storage head installation"), |
| 355 | ProductionType("B11", "Hydro Run-of-river head installation"), | 355 | ProductionType("B11", "Hydro Run-of-river head installation"), |
| 356 | ProductionType("B12", "Hydro-electric storage head installation"), | 356 | ProductionType("B12", "Hydro-electric storage head installation"), |
| 357 | ProductionType("A04", "Generation"), | 357 | ProductionType("A04", "Generation"), |
| 358 | production_type_B37, | 358 | production_type_B37, |
| 359 | production_type_B30, | 359 | production_type_B30, |
| 360 | ] | 360 | ] |
| 361 | ) | 361 | ) |
| 362 | </code> | 362 | </code> |
| 363 | </pre> | 363 | </pre> |
| @@ -371,21 +371,21 @@ | |||
| 371 | # adding some production plans... | 371 | # adding some production plans... |
| 372 | 372 | ||
| 373 | session.add( | 373 | session.add( |
| 374 | ProductionPlan( | 374 | ProductionPlan( |
| 375 | datetime.datetime.now(), | 375 | datetime.datetime.now(), |
| 376 | datetime.datetime(2022, 11, 2, 1, 0), | 376 | datetime.datetime(2022, 11, 2, 1, 0), |
| 377 | production_type_B37, | 377 | production_type_B37, |
| 378 | bidding_area1, | 378 | bidding_area1, |
| 379 | decimal.Decimal("80.5"), | 379 | decimal.Decimal("80.5"), |
| 380 | ) | 380 | ) |
| 381 | ) | 381 | ) |
| 382 | 382 | ||
| 383 | production_plan2 = ProductionPlan( | 383 | production_plan2 = ProductionPlan( |
| 384 | datetime.datetime.now(), | 384 | datetime.datetime.now(), |
| 385 | datetime.datetime(2022, 11, 2, 2, 0), | 385 | datetime.datetime(2022, 11, 2, 2, 0), |
| 386 | production_type_B37, | 386 | production_type_B37, |
| 387 | bidding_area1, | 387 | bidding_area1, |
| 388 | decimal.Decimal("90.5"), | 388 | decimal.Decimal("90.5"), |
| 389 | ) | 389 | ) |
| 390 | 390 | ||
| 391 | session.add(production_plan2) | 391 | session.add(production_plan2) |
| @@ -427,17 +427,17 @@ | |||
| 427 | 427 | ||
| 428 | # return production plans with production type 'B37' | 428 | # return production plans with production type 'B37' |
| 429 | session.query(ProductionPlan).filter( | 429 | session.query(ProductionPlan).filter( |
| 430 | ProductionPlan.production_type_id == ProductionType.production_type_id | 430 | ProductionPlan.production_type_id == ProductionType.production_type_id |
| 431 | ).filter(ProductionType.code == "B37").all() | 431 | ).filter(ProductionType.code == "B37").all() |
| 432 | session.query(ProductionPlan).join(ProductionType).filter(ProductionType.code == "B37").all() | 432 | session.query(ProductionPlan).join(ProductionType).filter(ProductionType.code == "B37").all() |
| 433 | session.query(ProductionPlan).filter(ProductionPlan.production_type == production_type_B37).all() | 433 | session.query(ProductionPlan).filter(ProductionPlan.production_type == production_type_B37).all() |
| 434 | session.query( | 434 | session.query( |
| 435 | ProductionPlan | 435 | ProductionPlan |
| 436 | ).from_statement( | 436 | ).from_statement( |
| 437 | text( | 437 | text( |
| 438 | "SELECT pp.* FROM production_plans pp, production_types pt " | 438 | "SELECT pp.* FROM production_plans pp, production_types pt " |
| 439 | "WHERE pp.production_type_id = pt.production_type_id AND pt.code=:code" | 439 | "WHERE pp.production_type_id = pt.production_type_id AND pt.code=:code" |
| 440 | ) | 440 | ) |
| 441 | ).params(code="B37").all() | 441 | ).params(code="B37").all() |
| 442 | </code> | 442 | </code> |
| 443 | </pre> | 443 | </pre> |
| @@ -449,29 +449,28 @@ | |||
| 449 | 449 | ||
| 450 | </div> | 450 | </div> |
| 451 | </div> | 451 | </div> |
| 452 | |||
| 453 | <script src="dist/reveal.js"></script> | ||
| 454 | <script src="plugin/zoom/zoom.js"></script> | ||
| 455 | <script src="plugin/notes/notes.js"></script> | ||
| 456 | <script src="plugin/search/search.js"></script> | ||
| 457 | <script src="plugin/markdown/markdown.js"></script> | ||
| 458 | <script src="plugin/highlight/highlight.js"></script> | ||
| 459 | <script> | ||
| 460 | 452 | ||
| 461 | // Also available as an ES module, see: | 453 | <script src="dist/reveal.js"></script> |
| 462 | // https://revealjs.netlify.app/initialization/ | 454 | <script src="plugin/zoom/zoom.js"></script> |
| 463 | Reveal.initialize({ | 455 | <script src="plugin/notes/notes.js"></script> |
| 464 | controls: true, | 456 | <script src="plugin/search/search.js"></script> |
| 465 | progress: true, | 457 | <script src="plugin/markdown/markdown.js"></script> |
| 466 | center: true, | 458 | <script src="plugin/highlight/highlight.js"></script> |
| 467 | hash: true, | 459 | <script> |
| 460 | |||
| 461 | // Also available as an ES module, see: | ||
| 462 | // https://revealjs.com/initialization/ | ||
| 463 | Reveal.initialize({ | ||
| 464 | controls: true, | ||
| 465 | progress: true, | ||
| 466 | center: true, | ||
| 467 | hash: true, | ||
| 468 | 468 | ||
| 469 | // Learn about plugins: https://revealjs.netlify.app/plugins/ | 469 | // Learn about plugins: https://revealjs.com/plugins/ |
| 470 | plugins: [ RevealZoom, RevealNotes, RevealSearch, RevealMarkdown, RevealHighlight ] | 470 | plugins: [ RevealZoom, RevealNotes, RevealSearch, RevealMarkdown, RevealHighlight ] |
| 471 | }); | 471 | }); |
| 472 | Reveal.configure({ pdfSeparateFragments: false }); | ||
| 473 | 472 | ||
| 474 | </script> | 473 | </script> |
| 475 | 474 | ||
| 476 | </body> | 475 | </body> |
| 477 | </html> | 476 | </html> |
