mirror of
https://github.com/almet/notmyidea.git
synced 2025-04-28 19:42:37 +02:00
120 lines
No EOL
7.3 KiB
HTML
120 lines
No EOL
7.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<title>
|
|
Deploying and customizing datasette - Alexis Métaireau </title>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="stylesheet"
|
|
href="https://blog.notmyidea.org/theme/css/main.css?v2"
|
|
type="text/css" />
|
|
<link href="https://blog.notmyidea.org/feeds/all.atom.xml"
|
|
type="application/atom+xml"
|
|
rel="alternate"
|
|
title="Alexis Métaireau ATOM Feed" />
|
|
</head>
|
|
<body>
|
|
<div id="content">
|
|
<section id="links">
|
|
<ul>
|
|
<li>
|
|
<a class="main" href="/">Alexis Métaireau</a>
|
|
</li>
|
|
<li>
|
|
<a class=""
|
|
href="https://blog.notmyidea.org/journal/index.html">Journal</a>
|
|
</li>
|
|
<li>
|
|
<a class="selected"
|
|
href="https://blog.notmyidea.org/code/">Code, etc.</a>
|
|
</li>
|
|
<li>
|
|
<a class=""
|
|
href="https://blog.notmyidea.org/weeknotes/">Notes hebdo</a>
|
|
</li>
|
|
<li>
|
|
<a class=""
|
|
href="https://blog.notmyidea.org/lectures/">Lectures</a>
|
|
</li>
|
|
<li>
|
|
<a class=""
|
|
href="https://blog.notmyidea.org/projets.html">Projets</a>
|
|
</li>
|
|
</ul>
|
|
</section>
|
|
|
|
<header>
|
|
<h1 class="post-title">Deploying and customizing datasette</h1>
|
|
<p><em>Step by step follow-up on how I've deployed it and added custom templates on top.</em></p>
|
|
<time datetime="2023-11-12T00:00:00+01:00">12 novembre 2023</time>
|
|
|
|
|
|
</header>
|
|
<article>
|
|
<p>First, create the venv and install everything</p>
|
|
<div class="highlight"><pre><span></span><code><span class="c1"># Create and activate venv</span>
|
|
python3<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>venv
|
|
<span class="nb">source</span><span class="w"> </span>venv/bin/activate
|
|
|
|
<span class="c1"># Install datasette…</span>
|
|
pip<span class="w"> </span>install<span class="w"> </span>datasette
|
|
|
|
<span class="c1"># … and the plugins</span>
|
|
datasette<span class="w"> </span>install<span class="w"> </span>datasette-render-markdown<span class="w"> </span>datasette-dashboards<span class="w"> </span>datasette-dateutil
|
|
</code></pre></div>
|
|
|
|
<p>I was curious how much all of this was weighting. <span class="caps">30MB</span> seems pretty reasonable to me.</p>
|
|
<div class="highlight"><pre><span></span><code><span class="c1"># All of this weights 30Mb</span>
|
|
du<span class="w"> </span>-sh<span class="w"> </span>venv
|
|
30M<span class="w"> </span>venv
|
|
</code></pre></div>
|
|
|
|
<h2 id="adding-authentication">Adding authentication</h2>
|
|
<p>Datasette doesn’t provide authentication by default, so <a href="https://docs.datasette.io/en/stable/authentication.html">you have to use a plugin for this</a>. I’ll be using <a href="https://github.com/simonw/datasette-auth-github">Github authentication</a> for now as it seems simple to add:</p>
|
|
<div class="highlight"><pre><span></span><code>pip install datasette-auth-github
|
|
</code></pre></div>
|
|
|
|
<p>I’ve had to create a new github application and export the variables to my server, and add some configuration to my <code>metadata.yaml</code> file:</p>
|
|
<div class="highlight"><pre><span></span><code><span class="nt">allow</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="nt">gh_login</span><span class="p">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">almet</span>
|
|
|
|
<span class="nt">plugins</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="nt">datasette-auth-github</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="nt">client_id</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="s">"$env"</span><span class="p p-Indicator">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">GITHUB_CLIENT_ID</span>
|
|
<span class="w"> </span><span class="nt">client_secret</span><span class="p">:</span>
|
|
<span class="w"> </span><span class="s">"$env"</span><span class="p p-Indicator">:</span><span class="w"> </span><span class="l l-Scalar l-Scalar-Plain">GITHUB_CLIENT_SECRET</span>
|
|
</code></pre></div>
|
|
|
|
<p>If that’s useful to you, here is <a href="https://gitlab.com/almet/timetracker-datasette-deploy">the git repository</a> I’m deploying to my server.</p>
|
|
<h2 id="using-templates">Using templates</h2>
|
|
<p>Okay, I now want to be able to send an <span class="caps">URL</span> to the people I’m working with, on which they can see what I’ve been doing, and what I’ve been using my time on.</p>
|
|
<p>It was pretty simple to do, and kind of weird to basically do what I’ve been doing back in the days for my first <span class="caps">PHP</span> applications : put <span class="caps">SQL</span> statements in the templates ! heh.</p>
|
|
<p>I’ve added a template with what I want to do. It has the side-effect of being able to propose a read-only view to a private database.</p>
|
|
<div class="highlight"><pre><span></span><code><span class="p"><</span><span class="nt">h1</span><span class="p">></span>{{project}}
|
|
{% for row in sql("SELECT SUM(CAST(duration AS REAL)) as total_hours FROM journal WHERE project = '" + project + "';", database="db") %}
|
|
({{ row["total_hours"] }} heures)
|
|
{% endfor %}
|
|
<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
|
|
<span class="p"><</span><span class="nt">dl</span><span class="p">></span>
|
|
{% for row in sql("select date, CAST(duration AS REAL) as duration, content from journal where project = '" + project + "' order by date DESC", database="db") %}
|
|
<span class="p"><</span><span class="nt">dt</span><span class="p">></span>{{ row["date"] }} ({{ row["duration"] }} heures)<span class="p"></</span><span class="nt">dt</span><span class="p">></span>
|
|
<span class="p"><</span><span class="nt">dd</span><span class="p">></span>{{ render_markdown(row["content"]) }}<span class="p"></</span><span class="nt">dd</span><span class="p">></span>
|
|
{% endfor %}
|
|
<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
|
|
</code></pre></div>
|
|
|
|
<p>Which looks like this :</p>
|
|
<p><img alt="Alt text" src="/images/datasette/custom-template.png"></p>
|
|
<p>
|
|
<a href="https://blog.notmyidea.org/tag/datasette.html">#Datasette</a>, <a href="https://blog.notmyidea.org/tag/deployment.html">#Deployment</a> - Posté dans la catégorie <a href="https://blog.notmyidea.org/code/">code</a>
|
|
</p>
|
|
</article>
|
|
<footer>
|
|
<a id="feed" href="/feeds/all.atom.xml">
|
|
<img alt="RSS Logo" src="/theme/rss.svg" />
|
|
</a>
|
|
</footer>
|
|
</div>
|
|
</body>
|
|
</html> |