Skip to content
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# Vagrant stuff
.vagrant
40 changes: 16 additions & 24 deletions 001-simple-tr/reahl/simple-tr/simple_tr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from reahl.web.bootstrap.grid import Container
from reahl.web.bootstrap.forms import TextInput, Form, FormLayout, Button, ButtonLayout
from reahl.component.modelinterface import exposed, Field, EmailField, Action, Event
from reahl.sqlalchemysupport import Session, Base
from reahl.sqlalchemysupport import Session, Base, session_scoped
from sqlalchemy import Column, Integer, UnicodeText
from sqlalchemy.orm import relationship


@session_scoped
class TR(Base):
__tablename__ = 'simple_tr_tr'

Expand All @@ -29,24 +30,24 @@ def fields(self, fields):
fields.separator = Field(label='Separated by (Regular Expression)', required=True)
fields.joiner = Field(label='Join with (Character String)', required=True)

def save(self):
Session.add(self)

@classmethod
def has_session_data(cls):
record = Session.query(__class__).order_by(TR.id.desc()).first()
return record
@property
def has_data(self):
return self.input_text and self.separator and self.joiner

@exposed('save')
def events(self, events):
events.save = Event(label='Perform Tr', action=Action(self.save))
events.save = Event(label='Perform Tr')

@property
def translated_string(self):
import re
return re.sub(self.separator, self.joiner, self.input_text)


class MyPage(HTML5Page):
def __init__(self, view):
super(__class__, self).__init__(view)


self.body.use_layout(Container())

layout = ResponsiveLayout('md', colour_theme='dark', bg_scheme='primary')
Expand All @@ -59,17 +60,15 @@ def __init__(self, view):


class InputForm(Form):
def __init__(self, view):
def __init__(self, view, tr):
super(__class__, self).__init__(view, 'address_form')

inputs = self.add_child(FieldSet(view, legend_text='Enter data then click button'))
inputs.use_layout(FormLayout())

tr = TR(separator='\\s+', joiner='-')
inputs.layout.add_input(TextInput(self, tr.fields.input_text))
inputs.layout.add_input(TextInput(self, tr.fields.separator))
inputs.layout.add_input(TextInput(self, tr.fields.joiner))
self.tr = tr

button = inputs.add_child(Button(self, tr.events.save))
button.use_layout(ButtonLayout(style='primary'))
Expand All @@ -79,20 +78,13 @@ class MyPanel(Div):
def __init__(self, view):
super(__class__, self).__init__(view)

my_form = InputForm(view)
tr = TR.for_current_session()
my_form = InputForm(view, tr)
self.add_child(my_form)

self.add_child(OutputBox(view, my_form))

if tr.has_data:
self.add_child(P(view, text=tr.translated_string))

class OutputBox(Widget):
def __init__(self, view, form):
super(__class__, self).__init__(view)
tr = TR.has_session_data()
if tr is not None:
import re
new_str = re.sub(tr.separator, tr.joiner, tr.input_text)
self.add_child(P(view, text=new_str))

class MyUI(UserInterface):
def assemble(self):
Expand Down
22 changes: 22 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# To use vagrant on ubuntu:
# apt-get install vagrant vagrant-lxc vagrant-cachier
# cd <where VagrantFile is>
# vagrant up
# vagrant ssh

Vagrant.require_version ">= 1.8.1"

Vagrant.configure(2) do |config|
config.vm.box = "reahl/bionic64"

config.vm.network "forwarded_port", guest: 8000, host: 8000, auto_correct: true
config.vm.network "forwarded_port", guest: 8363, host: 8363, auto_correct: true

config.vm.provision "shell", privileged: false, inline: <<-SHELL
pip install reahl[all]
SHELL
end