Static Interactive Fiction Generator
Project description
Grimoire
Grimoire is a Python library for creating interactive fiction as hyperlinked html.
Installation
pip install grimoire-if
Usage
Check out the tutorial created in Grimoire itself (source).
Get started
Create an instance of a Grimoire app.
from grimoire import Grimoire
app = Grimoire()
Add your first page
Create a function decorated by your app's page method. Pass the keyword argument, start=True for the first page.
@app.page(start=True)
def start(state):
return "This is my first grimoire app.", state
Render your story
You can render our (rather boring) story right now by calling the app's render method.
# will create all the file in the /site directory
app.render()
# optionally pass an alternate path
app.render("docs/")
Inline html
The content your page function returns is rendered using Python's built-in str function, so you can include html directly in a string if you'd like.
Alternativley, Grimoire comes with a small library for creating html called hype. Import hype's classes and create html using only Python!
from hype import H1, P
@app.page(start=True)
def start(state):
return Div(
H1("My First Grimoire Story"),
"<p>Inline html as a string<p>",
P("Html using the Hype library")
), state
Add an option to your first page
Create another page function (we don't need start=True this time). To add this as an option to an existing page, pass an argument to the parent page which has the same name as the new page function. Use Grimoire's builtin link function to create a link to the page.
You can add as many options as you like by continuing to add arguments to a parent page's function signature.
from grimoire.templates import link
@app.page(start=True)
def start(state, second):
return Div(
P("Hello, Grimoire!"),
Ul(Li(link("Go to the second page", second)))
), state
@app.page()
def second(state):
return Div(
P("I'm the second page.")
), state
Manage your stories state
The state object passed to your page function can be read and updated to manage the state of your application. By default it's a dictionary.
Notice how the we access the message from the first page in the second page.
@app.page(start=True)
def start(state, second):
state["message"] = "Hello, traveller"
return Div(
P("Hello, Grimoire!"),
Ul(Li(link("Go to the second page", second)))
), state
@app.page()
def second(state):
return Div(
P(f"message: {state['message']}")
), state
Use a custom state class
Dictionaries are cool, but often a custom class will make writing our code much more enjoyable. You can add a custom state class when creating your app.
from dataclasses import dataclass
@dataclass
class State:
message: str = ""
app = Grimoire(State)
@app.page(start=True)
def start(state, second):
state.message = "Hello, traveller"
return Div(
P("Hello, Grimoire!"),
Ul(Li(link("Go to the second page", second)))
), state
@app.page()
def second(state):
return Div(
P(f"message: {state.message}")
), state
Back to the beginning
Circular references are easy in Grimoire. Just add the option argument for an eariler page.
Warning: Be careful about creating infinite loops. Grimoire will continue rendering pages as long as it's seeing a version of the state that hasn't previously been rendered.
@app.page(start=True)
def start(state, second):
state.message = "Hello, traveller"
return Div(
P("Hello, Grimoire!"),
Ul(Li(link("Go to the second page", second)))
), state
@app.page()
def second(state, start):
return Div(
P(f"message: {state.message}"),
Ul(Li(link("Start over", start)))
), state
Default page function
Grimoire comes packaged with a function to style your page and render your options by default. It returns a decorator which can be applied to your page functions.
from grimoire.templates import default_page
@app.page(start=True)
@default_page("Minimal Example")
def start(state, second):
state.message = "Hello, traveller"
return Div(
P("Hello, Grimoire!")
), [("Go to the second page", second)], state
# Also try changing up some of the colors
@app.page()
default_page(
"Minimal Example",
primary_bg_color="#ff0000",
secondary_bg_color="#00ff00"
font_color="#bbbbbb"
)
def second(state, start):
return Div(
P(f"message: {state.message}")
), [("Start over", start)], state
Next Steps
That's it! You've completed the Grimoire tutoiral. As you can see, there's not much to it. Grimiore is purposeley very minimal and our belief is that many features can be easily implemented using plain old vanilla Python on top of Grimoire.
Check some further examples:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file grimoire-if-1.0.7.tar.gz
.
File metadata
- Download URL: grimoire-if-1.0.7.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60a6e3ddea1a9f6255ecdf5a1b8ac90ac0e1cd4e5dbd4d42d914e8ebc88628cf |
|
MD5 | f30075c14d2174cdd880271e451d80b7 |
|
BLAKE2b-256 | 0b95275a16110596f19f8e22198a32904c0195599bd473134b3f034db89fe32e |
File details
Details for the file grimoire_if-1.0.7-py3-none-any.whl
.
File metadata
- Download URL: grimoire_if-1.0.7-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 877579e032209adfe62ebffd470d39bcf78bc1c922de3160e1d9b49d0a327bb5 |
|
MD5 | 94487b0ca9e2261b47b1c01b944d320c |
|
BLAKE2b-256 | 967347cf8a34867a94abd645405dbeba75b0c2f06ce3cca3ed99f9b0dafc1e09 |