<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pyramid</title>
	<atom:link href="https://techgrowup.net/tag/pyramid/feed/" rel="self" type="application/rss+xml" />
	<link>https://techgrowup.net</link>
	<description>エンジニアを強くする</description>
	<lastBuildDate>Tue, 17 Dec 2024 17:44:58 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://techgrowup.net/wp-content/uploads/2021/05/hp-icon-150x150.png</url>
	<title>pyramid</title>
	<link>https://techgrowup.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Python開発入門44 Pyramidフレームワークの使い方と特徴</title>
		<link>https://techgrowup.net/python-pyramid/</link>
					<comments>https://techgrowup.net/python-pyramid/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[techgrowup]]></dc:creator>
		<pubDate>Tue, 17 Dec 2024 17:44:57 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[pyramid]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://techgrowup.net/?p=2219</guid>

					<description><![CDATA[はじめに PythonにはさまざまなWebフレームワークがありますが、その中でもPyramidは、柔軟性とスケーラビリティを兼ね備えたフレームワークとして注目されています。Pyramidは、シンプルなアプリから大規模なシ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading"><strong>はじめに</strong></h1>



<p class="wp-block-paragraph">PythonにはさまざまなWebフレームワークがありますが、その中でも<strong>Pyramid</strong>は、柔軟性とスケーラビリティを兼ね備えたフレームワークとして注目されています。<br>Pyramidは、シンプルなアプリから大規模なシステムまで対応可能な設計が特徴で、開発者がプロジェクトの規模に応じた機能を選択できるようになっています。</p>



<p class="wp-block-paragraph">この記事では、Pyramidの基本的な使い方、主な特徴、そして実際のコード例を通じて、その利便性を解説します。</p>



<h2 class="wp-block-heading"><strong>Pyramidとは</strong></h2>



<h3 class="wp-block-heading"><strong>Pyramidの概要</strong></h3>



<p class="wp-block-paragraph">Pyramidは、<strong>PythonベースのWebフレームワーク</strong>で、以下のような特長を持っています：</p>



<ul class="wp-block-list">
<li><strong>軽量で柔軟</strong><br>必要な機能だけを選択して使える設計。</li>



<li><strong>スケーラビリティに優れる</strong><br>小規模プロジェクトから大規模プロジェクトまで対応可能。</li>



<li><strong>セキュアな設計</strong><br>認証や認可などのセキュリティ機能を簡単に組み込める。</li>
</ul>



<h3 class="wp-block-heading"><strong>Pyramidの主な特徴</strong></h3>



<ol class="wp-block-list">
<li><strong>柔軟なルーティング</strong><br>URL構造に柔軟に対応可能で、複雑なアプリケーションにも適応。</li>



<li><strong>プラグイン可能な設計</strong><br>必要な機能を追加できる拡張性の高いアーキテクチャ。</li>



<li><strong>豊富なセキュリティ機能</strong><br>認証と認可のための組み込みサポート。</li>



<li><strong>大規模プロジェクトに最適</strong><br>トランザクション管理やテンプレートエンジンの選択など、エンタープライズ向けの機能を提供。</li>
</ol>



<h2 class="wp-block-heading"><strong>Pyramidのインストールとセットアップ</strong></h2>



<p class="wp-block-paragraph">Pyramidを使い始めるには、以下の手順でインストールとセットアップを行います。</p>



<h3 class="wp-block-heading"><strong>Pyramidのインストール</strong></h3>



<p class="wp-block-paragraph">以下のコマンドでPyramidをインストールします：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="pip install &quot;pyramid==2.0&quot;" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">pip</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;pyramid==2.0&quot;</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>プロジェクトの作成</strong></h3>



<p class="wp-block-paragraph">Pyramidの公式ツールを使って新しいプロジェクトを作成します：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="pip install cookiecutter
cookiecutter gh:Pylons/pyramid-cookiecutter-starter" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">pip</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">cookiecutter</span></span>
<span class="line"><span style="color: #DCDCAA">cookiecutter</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">gh:Pylons/pyramid-cookiecutter-starter</span></span></code></pre></div>



<p class="wp-block-paragraph">このコマンドを実行すると、プロジェクト名やディレクトリ構造を指定するプロンプトが表示されます。</p>



<h3 class="wp-block-heading"><strong>3. サーバーの起動</strong></h3>



<p class="wp-block-paragraph">プロジェクトディレクトリに移動し、以下のコマンドで開発サーバーを起動します：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="pip install -e .
pserve development.ini" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">pip</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">-e</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">.</span></span>
<span class="line"><span style="color: #DCDCAA">pserve</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">development.ini</span></span></code></pre></div>



<p class="wp-block-paragraph">ブラウザで<code>http://localhost:6543</code>を開くと、Pyramidアプリが動作していることを確認できます。</p>



<h2 class="wp-block-heading"><strong>Pyramidの基本構造</strong></h2>



<p class="wp-block-paragraph">Pyramidプロジェクトは、以下のようなディレクトリ構造を持っています：</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="my_project/
├── my_project/
│   ├── __init__.py
│   ├── views.py
│   ├── static/
│   └── templates/
├── setup.py
├── development.ini
└── production.ini" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">my_project/</span></span>
<span class="line"><span style="color: #DCDCAA">├──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">my_project/</span></span>
<span class="line"><span style="color: #DCDCAA">│</span><span style="color: #D4D4D4">   </span><span style="color: #CE9178">├──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">__init__.py</span></span>
<span class="line"><span style="color: #DCDCAA">│</span><span style="color: #D4D4D4">   </span><span style="color: #CE9178">├──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">views.py</span></span>
<span class="line"><span style="color: #DCDCAA">│</span><span style="color: #D4D4D4">   </span><span style="color: #CE9178">├──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">static/</span></span>
<span class="line"><span style="color: #DCDCAA">│</span><span style="color: #D4D4D4">   </span><span style="color: #CE9178">└──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">templates/</span></span>
<span class="line"><span style="color: #DCDCAA">├──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">setup.py</span></span>
<span class="line"><span style="color: #DCDCAA">├──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">development.ini</span></span>
<span class="line"><span style="color: #DCDCAA">└──</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">production.ini</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong><code>views.py</code></strong></h3>



<p class="wp-block-paragraph">アプリケーションのロジックを定義するファイルです。ルートに対応するビュー関数を記述します。</p>



<h3 class="wp-block-heading"><strong><code>templates/</code></strong></h3>



<p class="wp-block-paragraph">HTMLテンプレートを格納するディレクトリです。Jinja2やChameleonなどのテンプレートエンジンを使用可能です。</p>



<h3 class="wp-block-heading"><strong><code>development.ini</code></strong></h3>



<p class="wp-block-paragraph">開発環境の設定ファイルで、デバッグモードやポート番号を定義します。</p>



<h2 class="wp-block-heading"><strong>Pyramidアプリケーションの基本構造</strong></h2>



<p class="wp-block-paragraph">以下は、シンプルなPyramidアプリケーションの例です。</p>



<h3 class="wp-block-heading"><strong>コード例：基本的なアプリ</strong></h3>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response(&quot;Hello, Pyramid!&quot;)

if __name__ == &quot;__main__&quot;:
    with Configurator() as config:
        config.add_route(&quot;home&quot;, &quot;/&quot;)
        config.add_view(hello_world, route_name=&quot;home&quot;)
        app = config.make_wsgi_app()
    from wsgiref.simple_server import make_server
    server = make_server(&quot;0.0.0.0&quot;, 6543, app)
    server.serve_forever()" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> pyramid.config </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> Configurator</span></span>
<span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> pyramid.response </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> Response</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">hello_world</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">request</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> Response(</span><span style="color: #CE9178">&quot;Hello, Pyramid!&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">__name__</span><span style="color: #D4D4D4"> == </span><span style="color: #CE9178">&quot;__main__&quot;</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">with</span><span style="color: #D4D4D4"> Configurator() </span><span style="color: #C586C0">as</span><span style="color: #D4D4D4"> config:</span></span>
<span class="line"><span style="color: #D4D4D4">        config.add_route(</span><span style="color: #CE9178">&quot;home&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;/&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">        config.add_view(hello_world, </span><span style="color: #9CDCFE">route_name</span><span style="color: #D4D4D4">=</span><span style="color: #CE9178">&quot;home&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">        app = config.make_wsgi_app()</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> wsgiref.simple_server </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> make_server</span></span>
<span class="line"><span style="color: #D4D4D4">    server = make_server(</span><span style="color: #CE9178">&quot;0.0.0.0&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">6543</span><span style="color: #D4D4D4">, app)</span></span>
<span class="line"><span style="color: #D4D4D4">    server.serve_forever()</span></span></code></pre></div>



<p class="wp-block-paragraph"><strong>説明：</strong></p>



<ol class="wp-block-list">
<li><strong>ルートの定義</strong><br><code>config.add_route("home", "/")</code>で<code>/</code>パスを定義。</li>



<li><strong>ビューの作成</strong><br><code>hello_world</code>関数でビューのロジックを定義。</li>



<li><strong>アプリケーションの起動</strong><br>WSGIサーバーを使ってアプリケーションを起動。</li>
</ol>



<h2 class="wp-block-heading"><strong>Pyramidの主な機能</strong></h2>



<h3 class="wp-block-heading"><strong>柔軟なルーティング</strong></h3>



<p class="wp-block-paragraph">Pyramidでは、名前付きルートを使って柔軟なルーティングが可能です。</p>



<h6 class="wp-block-heading"><strong>例：複数のルート定義</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="def about_view(request):
    return Response(&quot;About Page&quot;)

def contact_view(request):
    return Response(&quot;Contact Page&quot;)

with Configurator() as config:
    config.add_route(&quot;about&quot;, &quot;/about&quot;)
    config.add_view(about_view, route_name=&quot;about&quot;)
    config.add_route(&quot;contact&quot;, &quot;/contact&quot;)
    config.add_view(contact_view, route_name=&quot;contact&quot;)" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">about_view</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">request</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> Response(</span><span style="color: #CE9178">&quot;About Page&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">contact_view</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">request</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> Response(</span><span style="color: #CE9178">&quot;Contact Page&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #C586C0">with</span><span style="color: #D4D4D4"> Configurator() </span><span style="color: #C586C0">as</span><span style="color: #D4D4D4"> config:</span></span>
<span class="line"><span style="color: #D4D4D4">    config.add_route(</span><span style="color: #CE9178">&quot;about&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;/about&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    config.add_view(about_view, </span><span style="color: #9CDCFE">route_name</span><span style="color: #D4D4D4">=</span><span style="color: #CE9178">&quot;about&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    config.add_route(</span><span style="color: #CE9178">&quot;contact&quot;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&quot;/contact&quot;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">    config.add_view(contact_view, </span><span style="color: #9CDCFE">route_name</span><span style="color: #D4D4D4">=</span><span style="color: #CE9178">&quot;contact&quot;</span><span style="color: #D4D4D4">)</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>テンプレートエンジンの利用</strong></h3>



<p class="wp-block-paragraph">Pyramidは、さまざまなテンプレートエンジンをサポートしています。以下はJinja2を使った例です。</p>



<h6 class="wp-block-heading"><strong>例：テンプレートの使用</strong></h6>



<ol class="wp-block-list">
<li><strong>インストール</strong></li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="pip install pyramid_jinja2" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">pip</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">pyramid_jinja2</span></span></code></pre></div>



<ol start="2" class="wp-block-list">
<li><strong>テンプレートの作成</strong>（<code>templates/home.jinja2</code>)</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;
&lt;body&gt;
    &lt;h1&gt;{{ message }}&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">html</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;</span><span style="color: #569CD6">body</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #808080">&lt;</span><span style="color: #569CD6">h1</span><span style="color: #808080">&gt;</span><span style="color: #D4D4D4">{{ message }}</span><span style="color: #808080">&lt;/</span><span style="color: #569CD6">h1</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">body</span><span style="color: #808080">&gt;</span></span>
<span class="line"><span style="color: #808080">&lt;/</span><span style="color: #569CD6">html</span><span style="color: #808080">&gt;</span></span></code></pre></div>



<ol start="3" class="wp-block-list">
<li><strong>ビューでテンプレートを使用</strong></li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from pyramid.renderers import render_to_response

def home_view(request):
    return render_to_response(&quot;templates/home.jinja2&quot;, {&quot;message&quot;: &quot;Hello from Jinja2!&quot;})" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> pyramid.renderers </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> render_to_response</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">home_view</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">request</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> render_to_response(</span><span style="color: #CE9178">&quot;templates/home.jinja2&quot;</span><span style="color: #D4D4D4">, {</span><span style="color: #CE9178">&quot;message&quot;</span><span style="color: #D4D4D4">: </span><span style="color: #CE9178">&quot;Hello from Jinja2!&quot;</span><span style="color: #D4D4D4">})</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>認証と認可</strong></h3>



<p class="wp-block-paragraph">Pyramidは、認証と認可のための組み込み機能を提供します。</p>



<h6 class="wp-block-heading"><strong>例：セキュアなビューの作成</strong></h6>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="from pyramid.security import Allow, Everyone

class RootFactory:
    __acl__ = [(Allow, Everyone, &quot;view&quot;)]

def secure_view(request):
    return Response(&quot;Secure Content&quot;)" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">from</span><span style="color: #D4D4D4"> pyramid.security </span><span style="color: #C586C0">import</span><span style="color: #D4D4D4"> Allow, Everyone</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">class</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">RootFactory</span><span style="color: #D4D4D4">:</span></span>
<span class="line"><span style="color: #D4D4D4">    __acl__ = [(Allow, Everyone, </span><span style="color: #CE9178">&quot;view&quot;</span><span style="color: #D4D4D4">)]</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">def</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">secure_view</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">request</span><span style="color: #D4D4D4">):</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> Response(</span><span style="color: #CE9178">&quot;Secure Content&quot;</span><span style="color: #D4D4D4">)</span></span></code></pre></div>



<h2 class="wp-block-heading"><strong>Pyramidを使うメリット</strong></h2>



<ol class="wp-block-list">
<li><strong>小規模から大規模プロジェクトまで対応可能</strong><br>必要な機能を選択して組み込める柔軟性があります。</li>



<li><strong>セキュリティが強力</strong><br>認証と認可が簡単に設定可能。</li>



<li><strong>エンタープライズ向け機能</strong><br>トランザクションや設定管理がしやすい設計。</li>
</ol>



<h2 class="wp-block-heading"><strong>まとめ</strong></h2>



<p class="wp-block-paragraph">Pyramidは、シンプルで柔軟性に富んだPythonフレームワークであり、小規模なアプリケーションから大規模なシステムまで幅広く利用できます。この記事で紹介した基本構造や実践例を参考に、プロジェクトに最適な形でPyramidを活用してみてください！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techgrowup.net/python-pyramid/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: techgrowup.net @ 2026-06-27 06:53:42 by W3 Total Cache
-->