summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimeon Simeonov2021-02-25 12:04:05 +0100
committerSimeon Simeonov2021-02-25 12:04:05 +0100
commitdecc91569d1971c0cac0fb1c3a5f556a5b53cd7d (patch)
treee4c30f5d319d0057db199ea7d04c49ac8b5549af
parent97e8b1c6ed35e84097f035a056d1c938b9942d11 (diff)
Add reveal.js/sqlalchemy.html and reveal.js/dist/theme/fifty.css
-rw-r--r--reveal.js/dist/theme/fifty.css297
-rw-r--r--reveal.js/images/sqlalchemy/sqla_arch.pngbin0 -> 42731 bytes
-rwxr-xr-xreveal.js/sqlalchemy.html361
3 files changed, 658 insertions, 0 deletions
diff --git a/reveal.js/dist/theme/fifty.css b/reveal.js/dist/theme/fifty.css
new file mode 100644
index 0000000..a55b3c2
--- /dev/null
+++ b/reveal.js/dist/theme/fifty.css
@@ -0,0 +1,297 @@
1/**
2 * A simple theme for reveal.js presentations, similar
3 * to the default theme. The accent color is brown.
4 *
5 * This theme is Copyright (C) 2012-2013 Owen Versteeg, http://owenversteeg.com - it is MIT licensed.
6 */
7.reveal a {
8 line-height: 1.3em; }
9
10section.has-dark-background, section.has-dark-background h1, section.has-dark-background h2, section.has-dark-background h3, section.has-dark-background h4, section.has-dark-background h5, section.has-dark-background h6 {
11 color: #fff; }
12
13/*********************************************
14 * GLOBAL STYLES
15 *********************************************/
16:root {
17 --background-color: #F0F1EB;
18 --main-font: Palatino Linotype, Book Antiqua, Palatino, FreeSerif, serif;
19 --main-font-size: 40px;
20 --main-color: #000;
21 --block-margin: 20px;
22 --heading-margin: 0 0 20px 0;
23 --heading-font: Palatino Linotype, Book Antiqua, Palatino, FreeSerif, serif;
24 --heading-color: #383D3D;
25 --heading-line-height: 1.2;
26 --heading-letter-spacing: normal;
27 --heading-text-transform: none;
28 --heading-text-shadow: none;
29 --heading-font-weight: normal;
30 --heading1-text-shadow: none;
31 --heading1-size: 3.77em;
32 --heading2-size: 2.11em;
33 --heading3-size: 1.55em;
34 --heading4-size: 1em;
35 --code-font: monospace;
36 --link-color: #51483D;
37 --link-color-hover: #8b7c69;
38 --selection-background-color: #26351C;
39 --selection-color: #fff; }
40
41.reveal-viewport {
42 background: #F0F1EB;
43 background-color: #F0F1EB; }
44
45.reveal {
46 font-family: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif;
47 font-size: 24px;
48 font-weight: normal;
49 color: #000; }
50
51.reveal ::selection {
52 color: #fff;
53 background: #26351C;
54 text-shadow: none; }
55
56.reveal ::-moz-selection {
57 color: #fff;
58 background: #26351C;
59 text-shadow: none; }
60
61.reveal .slides section,
62.reveal .slides section > section {
63 line-height: 1.3;
64 font-weight: inherit; }
65
66/*********************************************
67 * HEADERS
68 *********************************************/
69.reveal h1,
70.reveal h2,
71.reveal h3,
72.reveal h4,
73.reveal h5,
74.reveal h6 {
75 margin: 0 0 20px 0;
76 color: #383D3D;
77 font-family: "Palatino Linotype", "Book Antiqua", Palatino, FreeSerif, serif;
78 font-weight: normal;
79 line-height: 1.2;
80 letter-spacing: normal;
81 text-transform: none;
82 text-shadow: none;
83 word-wrap: break-word; }
84
85.reveal h1 {
86 font-size: 3.77em; }
87
88.reveal h2 {
89 font-size: 2.11em; }
90
91.reveal h3 {
92 font-size: 1.55em; }
93
94.reveal h4 {
95 font-size: 1em; }
96
97.reveal h1 {
98 text-shadow: none; }
99
100/*********************************************
101 * OTHER
102 *********************************************/
103.reveal p {
104 margin: 20px 0;
105 line-height: 1.3; }
106
107/* Remove trailing margins after titles */
108.reveal h1:last-child,
109.reveal h2:last-child,
110.reveal h3:last-child,
111.reveal h4:last-child,
112.reveal h5:last-child,
113.reveal h6:last-child {
114 margin-bottom: 0; }
115
116/* Ensure certain elements are never larger than the slide itself */
117.reveal img,
118.reveal video,
119.reveal iframe {
120 max-width: 95%;
121 max-height: 95%; }
122
123.reveal strong,
124.reveal b {
125 font-weight: bold; }
126
127.reveal em {
128 font-style: italic; }
129
130.reveal ol,
131.reveal dl,
132.reveal ul {
133 display: inline-block;
134 text-align: left;
135 margin: 0 0 0 1em; }
136
137.reveal ol {
138 list-style-type: decimal; }
139
140.reveal ul {
141 list-style-type: disc; }
142
143.reveal ul ul {
144 list-style-type: square; }
145
146.reveal ul ul ul {
147 list-style-type: circle; }
148
149.reveal ul ul,
150.reveal ul ol,
151.reveal ol ol,
152.reveal ol ul {
153 display: block;
154 margin-left: 40px; }
155
156.reveal dt {
157 font-weight: bold; }
158
159.reveal dd {
160 margin-left: 40px; }
161
162.reveal blockquote {
163 display: block;
164 position: relative;
165 width: 70%;
166 margin: 20px auto;
167 padding: 5px;
168 font-style: italic;
169 background: rgba(255, 255, 255, 0.05);
170 box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.2); }
171
172.reveal blockquote p:first-child,
173.reveal blockquote p:last-child {
174 display: inline-block; }
175
176.reveal q {
177 font-style: italic; }
178
179.reveal pre {
180 display: block;
181 position: relative;
182 width: 90%;
183 margin: 20px auto;
184 text-align: left;
185 font-size: 0.55em;
186 font-family: monospace;
187 line-height: 1.1em;
188 word-wrap: break-word; }
189 /* box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.15); } */
190
191.reveal code {
192 font-family: monospace;
193 text-transform: none; }
194
195.reveal pre code {
196 display: block;
197 padding: 4px;
198 overflow: auto;
199 max-height: 520px;
200 word-wrap: normal; }
201
202.reveal table {
203 margin: auto;
204 border-collapse: collapse;
205 border-spacing: 0; }
206
207.reveal table th {
208 font-weight: bold; }
209
210.reveal table th,
211.reveal table td {
212 text-align: left;
213 padding: 0.2em 0.5em 0.2em 0.5em;
214 border-bottom: 1px solid; }
215
216.reveal table th[align="center"],
217.reveal table td[align="center"] {
218 text-align: center; }
219
220.reveal table th[align="right"],
221.reveal table td[align="right"] {
222 text-align: right; }
223
224.reveal table tbody tr:last-child th,
225.reveal table tbody tr:last-child td {
226 border-bottom: none; }
227
228.reveal sup {
229 vertical-align: super;
230 font-size: smaller; }
231
232.reveal sub {
233 vertical-align: sub;
234 font-size: smaller; }
235
236.reveal small {
237 display: inline-block;
238 font-size: 0.6em;
239 line-height: 1.2em;
240 vertical-align: top; }
241
242.reveal small * {
243 vertical-align: top; }
244
245.reveal img {
246 margin: 20px 0; }
247
248/*********************************************
249 * LINKS
250 *********************************************/
251.reveal a {
252 color: #51483D;
253 text-decoration: none;
254 transition: color .15s ease; }
255
256.reveal a:hover {
257 color: #8b7c69;
258 text-shadow: none;
259 border: none; }
260
261.reveal .roll span:after {
262 color: #fff;
263 background: #25211c; }
264
265/*********************************************
266 * Frame helper
267 *********************************************/
268.reveal .r-frame {
269 border: 4px solid #000;
270 box-shadow: 0 0 10px rgba(0, 0, 0, 0.15); }
271
272.reveal a .r-frame {
273 transition: all .15s linear; }
274
275.reveal a:hover .r-frame {
276 border-color: #51483D;
277 box-shadow: 0 0 20px rgba(0, 0, 0, 0.55); }
278
279/*********************************************
280 * NAVIGATION CONTROLS
281 *********************************************/
282.reveal .controls {
283 color: #51483D; }
284
285/*********************************************
286 * PROGRESS BAR
287 *********************************************/
288.reveal .progress {
289 background: rgba(0, 0, 0, 0.2);
290 color: #51483D; }
291
292/*********************************************
293 * PRINT BACKGROUND
294 *********************************************/
295@media print {
296 .backgrounds {
297 background-color: #F0F1EB; } }
diff --git a/reveal.js/images/sqlalchemy/sqla_arch.png b/reveal.js/images/sqlalchemy/sqla_arch.png
new file mode 100644
index 0000000..a1c0958
--- /dev/null
+++ b/reveal.js/images/sqlalchemy/sqla_arch.png
Binary files differ
diff --git a/reveal.js/sqlalchemy.html b/reveal.js/sqlalchemy.html
new file mode 100755
index 0000000..956538f
--- /dev/null
+++ b/reveal.js/sqlalchemy.html
@@ -0,0 +1,361 @@
1<!doctype html>
2<html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <title>SQLAlchemy &amp; Odin Data Access</title>
6 <meta name="author" content="Simeon Simeonov">
7 <meta name="apple-mobile-web-app-capable" content="yes">
8 <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
9 <meta name="viewport" content="width=device-width, initial-scale=1.0">
10
11 <link rel="stylesheet" href="dist/reset.css">
12 <link rel="stylesheet" href="dist/reveal.css">
13
14 <link rel="stylesheet" href="dist/theme/fifty.css" id="theme">
15
16 <!-- Theme used for syntax highlighting of code -->
17 <link rel="stylesheet" href="plugin/highlight/monokai.css" id="highlight-theme">
18 <!-- <link rel="stylesheet" href="plugin/highlight/zenburn.css" id="highlight-theme"> -->
19 </head>
20 <body>
21 <div class="reveal">
22
23 <!-- Any section element inside of this container is displayed as a slide -->
24 <div class="slides">
25
26 <section>
27 <h2>SQLAlchemy &amp; Odin Data Access</h2>
28 <h4>Fifty Data Science</h4>
29 </br>
30 <p><small>Simeon Simeonov</small></p>
31 </section>
32
33 <section>
34
35 <section id="fragments">
36 <h2>Agenda</h2>
37 </br>
38 <ul>
39 <span class="fragment"><li>SQLAlchemy - Design & overview</li></span>
40 <span class="fragment"><li>SQLAlchemy - A small practical example</li></span>
41 <span class="fragment"><li>Odin Data Access - Overview and examples</li></span>
42 <span class="fragment"><li>Q &amp; A</li></span>
43 </ul>
44 </section>
45
46 </section>
47
48 <section>
49 <h2>What is SQLAlchemy?</h2>
50 </br>
51 <p><em>SQLAlchemy</em> is a Python library created by Mike Bayer to provide a high-level Pythonic interface to RDBMS such as <em>PostgreSQL</em>, <em>SQLite</em>, <em>MySQL</em>, <em>Oracle</em>, <em>DB2</em>.</p>
52 <p><em>SQLAlchemy</em> includes RDBMS-independent SQL expression language and an <em>object-relational mapper (ORM)</em>.</p>
53 </section>
54
55 <section>
56 <h2>Why use SQLAlchemy?</h2>
57 </br>
58 <ul>
59 <li><p>portability - the programming interface is independent of the type of RDBMS and connector used</p></li>
60 <li><p>security - no more SQL injections</p></li>
61 <li><p>abstraction - no need to bother with complex JOINs</p></li>
62 <li><p>object-orientation - you work with objects and NOT tables and rows</p></li>
63 <li><p>performance - exploits the likehood of reusing a particular query</p></li>
64 <li><p>flexibility - you can override almost anything</p></li>
65 </ul>
66 </section>
67
68 <section>
69 <h2>Basic architecture</h2>
70 <p><em>SQLAlchemy</em> consists of several components, including the SQL expression language and the ORM.</p>
71 <p>In order to enable these components, <em>SQLAlchemy</em> also provides an <em>Engine</em> class and <em>MetaData</em> class.</p>
72 <ul>
73 <li><em>Engine</em>- manages the <em>SQLAlchemy</em> connection pool and the database-independent SQL dialect layer</li>
74 <li><em>MetaData</em> - used to collect and organize information about your table layout (schema)</li>
75 <li><em>Session</em> - establishes all conversations with the RDBMS and represents a "holding zone" for all the objects which you've loaded or associated with it during its lifespan</li>
76 <li><em>SQL expression language</em> - provides an API to execute your queries and updates against your tables, all from Python, and all in a database-independent way (low-level interface)</li>
77 <li><em>ORM</em> - provides a convenient way to add database persistence to your Python objects withoutrequiring you to design your objects around the database, or the database around the objects (high-level interface)</li>
78 </ul>
79 <img src="images/sqlalchemy/sqla_arch.png"></img>
80 </section>
81
82 <section data-auto-animate>
83 <h2>Example</h2>
84 <p><em>SQLAlchemy</em> gives us the choice between <em>classical mapping</em> and the newer <em>declarative mapping</em></p>
85 <pre data-id="code-animation">
86 <code class="python" data-trim data-line-numbers type="text/template">
87 import sqlalchemy
88 sqlalchemy.__version__
89
90 from sqlalchemy import create_engine
91
92 # engine = create_engine("postgresql+psycopg2://user:zipassword@localhost/mydb" , echo=True)
93 # The string form of the URL is dialect+driver://user:password@host/dbname[?key=value..],
94 # where dialect is a database name such as mysql, oracle, postgresql, etc.,
95 # and driver the name of a DBAPI, such as psycopg2, pyodbc, cx_oracle
96 # The echo flag is a shortcut to setting up SQLAlchemy logging,
97 # which is accomplished via Python’s standard logging module.
98 # With it enabled, we’ll see all the generated SQL produced.
99 # engine = create_engine("sqlite:///library.db", echo=True)
100 engine = create_engine("sqlite:///:memory:", echo=True)
101
102 metadata = MetaData()
103
104 from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
105
106 authors_table = Table(
107 "authors",
108 metadata,
109 Column("id", Integer, primary_key=True),
110 Column("name", String),
111 ) # Column("name", String(50)) is possible
112
113 books_table = Table(
114 "books",
115 metadata,
116 Column("id", Integer, primary_key=True),
117 Column("title", String),
118 Column("description", String),
119 Column("author_id", ForeignKey('authors.id')),
120 )
121
122 metadata.create_all(engine) # creates the tables
123 </code>
124 </pre>
125 </section>
126
127 <section data-auto-animate>
128 <h2>Example (cont...)</h2>
129 <h4 data-id="code-title">Use of <em>SQL expression language</em></h4>
130 <pre data-id="code-animation">
131 <code class="python" data-trim data-line-numbers type="text/template">
132 insert_stmt = authors_table.insert(bind=engine)
133 type(insert_stmt)
134 # Out: &lt;class 'sqlalchemy.sql.expression.Insert'&gt;
135 print(insert_stmt)
136 # Out: INSERT INTO authors (id, name) VALUES (:id,:name)
137
138 compiled_stmt = insert_stmt.compile()
139 print(compiled_stmt.params)
140 # Out: {'id': None, 'name': None}
141
142 insert_stmt.execute(name="Alexandre Dumas") # insert a single entry
143 insert_stmt.execute([{"name": 'Mr X'},{'name': 'Mr Y'}]) # a list of entries
144
145 metadata.bind = engine # no need to explicitly bind the engine from now on
146 select_stmt = authors_table.select(authors_table.c.id==2)
147 result = select_stmt.execute()
148 result.fetchall()
149 # Out: [(1, u'Mr X')]
150
151 del_stmt = authors_table.delete()
152 del_stmt.execute(whereclause=text("name='Mr Y'"))
153 del_stmt.execute() # delete all
154 </code>
155 </pre>
156 </section>
157
158 <section data-auto-animate>
159 <h2>Example (cont...)</h2>
160 <h4 data-id="code-title">Use of <em>classical mapping</em></h4>
161 <pre data-id="code-animation">
162 <code class="python" data-trim data-line-numbers type="text/template">
163 from sqlalchemy.orm import mapper
164 from sqlalchemy.orm import relationship, backref
165
166 class Author:
167 def __init__(self, name):
168 self.name = name
169
170 def __str__(self):
171 return self.name
172
173
174 class Book:
175 def __init__(self, title, description, author):
176 self.title = title
177 self.description = description
178 self.author = author
179
180 def __str__(self):
181 return self.title
182
183 mapper(Book, books_table)
184 mapper(Author, authors_table, properties = {"books": relation(Book, backref="author")})
185 </code>
186 </pre>
187 </section>
188
189 <section data-auto-animate>
190 <h2>Example (cont...)</h2>
191 <h4 data-id="code-title">Doing the same thing the easy way with <em>declarative mapping</em></h4>
192 <pre data-id="code-animation">
193 <code class="python" data-trim data-line-numbers type="text/template">
194 from sqlalchemy.ext.declarative import declarative_base
195 from sqlalchemy.orm import relationship, backref
196
197 Base = declarative_base()
198
199 class Author(Base):
200 __tablename__ = "authors"
201
202 id = Column(Integer, primary_key=True)
203 name = Column(String)
204
205 def __init__(self, name):
206 self.name = name
207
208 def __str__(self):
209 return self.name
210
211
212 class Book(Base):
213 __tablename__ = "books" # self.__table__ will be available for our objects
214
215 id = Column(Integer, primary_key=True)
216 title = Column(String)
217 description = Column(String)
218 author_id = Column(Integer, ForeignKey("authors.id"))
219 author = relationship(Author, backref=backref("books", order_by=title))
220
221 def __init__(self, title, description, author):
222 self.title = title
223 self.description = description
224 self.author = author
225
226 def __str__(self):
227 return self.title
228
229 Base.metadata.create_all(engine)# create tables
230 </code>
231 </pre>
232 </section>
233
234 <section data-auto-animate>
235 <h2>Example (cont...)</h2>
236 <h4 data-id="code-title">Creating instances</h4>
237 <pre data-id="code-animation">
238 <code class="python" data-trim data-line-numbers type="text/template">
239 from sqlalchemy.orm import sessionmaker
240
241 Session = sessionmaker(bind=engine) # bound session
242 session = Session()
243
244 author_1 = Author("Richard Dawkins")
245 author_2 = Author("Matt Ridley")
246
247 book_1 = Book("The Red Queen", "A popular science book", author_2)
248 book_2 = Book("The Selfish Gene", "A popular science book", author_1)
249 book_3 = Book("The Blind Watchmaker", "The theory of evolutio", author_1) # typo
250
251 session.add(author_1)
252 session.add(author_2)
253 session.add(book_1)
254 session.add(book_2)
255 session.add(book_3) # or simply session.add_all([author_1, author_2, book_1, book_2, book_3])
256
257 # session.flush()
258 session.commit() # flushes (issues the statements and sends them to the RDBMS) and commits
259
260 book_3.description = "The theory of evolution" # update the object
261 book_3 in session # check whether the object is in the session
262 # Out: True
263
264 session.commit()
265 </code>
266 </pre>
267 </section>
268
269 <section data-auto-animate>
270 <h2>Example (cont...)</h2>
271 <h4 data-id="code-title">Queries</h4>
272 <pre data-id="code-animation">
273 <code class="python" data-trim data-line-numbers type="text/template">
274 session.query(Book).order_by(Book.id) # returns a Query instance with a .statement attribute
275 session.query(Book).order_by(Book.id).all() # returns an object-list
276
277 # return all book objects where title == "The Selfish Gene"
278 session.query(Book).filter(Book.title == "The Selfish Gene").order_by(Book.id).all()
279
280 # using LIKE
281 session.query(Book).filter(Book.title.like("The%")).order_by(Book.id).all()
282
283 query = session.query(Book).filter(Book.id == 9).order_by(Book.id)
284 query.count() # returns 0L
285 query.all() # returns an empty list
286 query.first() # returns None
287 query.one() # raises NoResultFound exception
288
289 query = session.query(Book).filter(Book.id == 1).order_by(Book.id)
290 book_1 = query.one()
291 book_1.description # returns "A popular science book"
292 book_1.author.books # returns a list of Book-objects representing all the books from the same author.
293
294 # get a list of all Book-instances where the author"s name is "Richard Dawkins"
295 session.query(Book).filter(Book.author_id == Author.id).filter(Author.name == "Richard Dawkins").all()
296 session.query(Book).join(Author).filter(Author.name == "Richard Dawkins").all()
297 session.query(Book).\
298 from_statement("SELECT b.* FROM books b, authors a WHERE b.author_id = a.id AND a.name=:name").\
299 params(name="Richard Dawkins").all()
300 </code>
301 </pre>
302 </section>
303
304 <section data-background="http://i.giphy.com/90F8aUepslB84.gif">
305 <h2>... simply amazing!</h2>
306 </section>
307
308 <section>
309 <h2>Odin Data Access</h2>
310 <p><a href="https://gitlab.fifty.eu/odin/data-science/odin-data-access">https://gitlab.fifty.eu/odin/data-science/odin-data-access</a></p>
311 <p>Provides the <em>odin_data_access</em> module with some of the following entities:</p>
312 <ul>
313 <li><em>session</em> module for creating Session objects in a Fifty environment via <em>get_session</em> and <em>get_session_from_env</em></li>
314 <li><em>DBBase</em> - a tiny base-class that can be used as a mixin for mapping</li>
315 <li><em>DBTableAccessor</em> - tiny wrapper for <em>Session</em> and <em>Table</em> as well a "facade" for <em>Query</em> and <em>pandas</em></li>
316 </ul>
317 <p>The <em>DBTableAccessor</em>:</p>
318 <ul>
319 <li>represents a single RDBMS-table that is introspected during instance initialization (<em>__init__</em>)</li>
320 <li>uses a mix of <em>ORM</em> and <em>SQL expression language</em> functionality</li>
321 <li>contins a mapper for mapping custom classes to its corresponding <em>Table</em></li>
322 <span class="fragment"><li>used by Batman and Chuck Norris (source: incomplete Google search)</li></span>
323 </ul>
324 </section>
325
326 <section>
327 <h1>Demo</h1>
328 <p>Now watch this well-rehearsed demo...</p>
329 </section>
330
331 <section>
332 <h1>Q &amp; A</h1>
333 </section>
334
335 </div>
336 </div>
337
338 <script src="dist/reveal.js"></script>
339 <script src="plugin/zoom/zoom.js"></script>
340 <script src="plugin/notes/notes.js"></script>
341 <script src="plugin/search/search.js"></script>
342 <script src="plugin/markdown/markdown.js"></script>
343 <script src="plugin/highlight/highlight.js"></script>
344 <script>
345
346 // Also available as an ES module, see:
347 // https://revealjs.netlify.app/initialization/
348 Reveal.initialize({
349 controls: true,
350 progress: true,
351 center: true,
352 hash: true,
353
354 // Learn about plugins: https://revealjs.netlify.app/plugins/
355 plugins: [ RevealZoom, RevealNotes, RevealSearch, RevealMarkdown, RevealHighlight ]
356 });
357
358 </script>
359
360 </body>
361</html>