\documentclass{lug} \title{Bottle} \author{Jack Rosenthal} \date{2017-03-23} \institute{Mines Linux Users Group} \begin{document} \begin{frame}{HTTP Crash Course} \begin{itemize}[<+->] \item \texttt{GET} -- Your typical ``downloads from a web page'' HTTP request \begin{itemize} \item Has no support for sending data to the server other than the URL requested \end{itemize} \item \texttt{POST} -- Your typical ``form submission'' HTTP request \begin{itemize} \item Sends multipart form data along with the request \item Web browsers ask you to ``resubmit'' when refreshed \end{itemize} \item \texttt{PUT} -- Ask to put a file at a location \begin{itemize} \item Not needed as the same data can be sent using \texttt{POST} \item No support in older browsers \item Simple to use when there's no web browser involved \end{itemize} \end{itemize} \end{frame} \begin{frame}{Decorators} Decorators are a pretty way to wrap functions using functions that return functions. \pause Both the following are equivalent: \inputminted{python3}{examples/decorator_equiv.py} Bottle makes heavy use of decorators to bind into routing. \end{frame} \begin{frame}{Bottle: Really Simple Web Framework} \begin{columns} \begin{column}{0.7\linewidth} \begin{itemize}[<+->] \item Provides routing and convenient access to data \item Built in HTTP server, or use any WSGI-compatible web server \item Very lightweight, only a couple thousand lines of code \end{itemize} \end{column} \begin{column}{0.3\linewidth} \includegraphics[width=\linewidth]{graphics/bottle} \end{column} \end{columns} \pause[\thebeamerpauses] \bigskip \begin{block}{A \emph{Hello, World!} App} \begin{minipage}{\linewidth} \tiny \inputminted{python3}{examples/bottle_ex.py} \end{minipage} \end{block} \end{frame} \begin{frame}{Where should I use Bottle?} \textbf{What Bottle Is} \begin{itemize}[<+->] \item Bottle is a \emph{micro-framework} \item Bottle is only a library \item Bottle really small and fast \end{itemize} \pause[\thebeamerpauses] \textbf{What Bottle Is Not} \begin{itemize}[<+->] \item Bottle is not a MVC framework \item Bottle will not generate files for you \item \emph{No magic included!} \end{itemize} \end{frame} \begin{frame}{Routing} Routing is how you let Bottle know which URLs map to which functions. Bottle uses decorators to specify this. \inputminted{python3}{examples/route_simple.py} \pause \medskip You can specify multiple routes per function, even using default arguments. \inputminted{python3}{examples/multiroute.py} \end{frame} \begin{frame}{More Routing} URLs match more specific routes before more generalized. \inputminted{python3}{examples/generalized.py} \medskip So \texttt{/page/WikiPage} will use \texttt{wiki\_page}, but \texttt{/jrosenth/home} will use \texttt{user\_page}. \end{frame} \begin{frame}{Routing Modifiers} You can modify parameters to only match certain types. \begin{itemize}[<+->] \item \texttt{:int} -- Only match integers \item \texttt{:float} -- Only match real numbers \item \texttt{:path} -- Match path to end of URL (including slashes) \item \texttt{:re} -- Match a regular expression \end{itemize} \pause[\thebeamerpauses] \medskip \inputminted{python3}{examples/route_modifiers.py} \end{frame} \begin{frame}{Routing Methods} The \texttt{route} decorator accepts all HTTP methods by default, you can use any of the alternate decorators to accept specific methods. \medskip \inputminted{python3}{examples/route_methods.py} \end{frame} \begin{frame}{Error Routes} The \texttt{error} decorator matches certain HTTP errors. \medskip \inputminted{python3}{examples/route_error.py} \end{frame} \begin{frame}{Abort! Abort!} Need to cause an error? \texttt{abort} is your amigo. \inputminted{python3}{examples/abort.py} \end{frame} \begin{frame}{Redirect} Perhaps you wanted to redirect them instead? \inputminted{python3}{examples/redirect.py} \end{frame} \begin{frame}{Serving up a static directory} \inputminted{python3}{examples/static_files.py} \end{frame} \begin{frame}{Accessing \texttt{POST} data} The \texttt{request} object gives you access to information about the request, including a dictionary contating form data called \texttt{forms}. \inputminted{python3}{examples/form_data.py} \end{frame} \begin{frame}{Mmm! Cookies!} \inputminted{python3}{examples/cookies.py} \end{frame} \begin{frame}{So what can you \texttt{return}?} \begin{itemize}[<+->] \item Returning a string will simply show the string for you \item You can return a dictionary and it will dump JSON for you \item Returning file objects (or anything with \texttt{.read()}) will show the file contents for you \item You can even return an iterable or \texttt{yield} in your function and it will continually pull and show \end{itemize} \end{frame} \begin{frame}{Templates} \begin{onlyenv}<1> Bottle includes a templating engine called \texttt{SimpleTemplate}. You can use it like this: \end{onlyenv} {\small \inputminted{python3}{examples/template.py} } \begin{onlyenv}<2> \begin{block}{\texttt{views/hello\_template.tpl}} \small \inputminted{html}{examples/hello_template.tpl} \end{block} \end{onlyenv} \end{frame} \begin{frame}{Applications and Sub-applications} \small \inputminted{python3}{examples/apps.py} \end{frame} \begin{frame}{Deploying your application} Bottle has many ways to deploy, here's the two most common: \begin{enumerate} \item Using WSGI, simply create an \texttt{app.wsgi} file that imports your bottle app as \texttt{application} \item Using CGI (compatible with nearly any web server), just put \mintinline{python3}{run(server='cgi')} in your CGI script \end{enumerate} \end{frame} \begin{frame}[standout] \Huge Questions? \end{frame} \end{document}