Mutable BeautifulSoup database type
Examples
Setup
from sqlalchemy_mutablesoup import MutableSoupType
from sqlalchemy import Column, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
# standard session creation
engine = create_engine('sqlite:///:memory:')
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
session = Session()
Base = declarative_base()
# define and instantiate a model with a MutableSoupType column.
class Model(Base):
__tablename__ = 'model'
id = Column(Integer, primary_key=True)
soup = Column(MutableSoupType)
Base.metadata.create_all(engine)
model = Model()
session.add(model)
Setting soup objects
model.soup = '<p>Hello World.</p>'
model.soup
Out:
<p>Hello World.</p>
Setting soup elements: basic use
model.soup.set_element(parent_selector='p', val='Hello Moon.')
session.commit()
model.soup
Out:
<p>Hello Moon.</p>
Creating soup elements with a gen_target
function
def gen_span_tag(*args, **kwargs):
print('My args are:', args)
print('My kwargs are:', kwargs)
return '<span></span>'
model.soup.set_element(
parent_selector='p',
val='Span text',
target_selector='span',
gen_target=gen_span_tag,
args=['hello world'],
kwargs={'hello': 'moon'},
)
session.commit()
model.soup
Out:
My args are: ('hello world',)
My kwargs are: {'hello': 'moon'}
<p>Hello Moon.<span>Span text</span></p>
Registering changes
Call changed
to register other changes with the database.
model.soup.select_one('p')['style'] = 'color:red;'
model.soup.changed()
session.commit()
model.soup
Out:
<p style="color:red;">Hello Moon.<span>Span text</span></p>
sqlalchemy_mutablesoup.SoupBase
Base for MutableSoup
objects. Interits from bs4.BeautifulSoup
.
Methods
text(self, selector) [source]
Get text from html element.
Parameters: | selector : str
CSS selector. |
---|---|
Returns: | text : str or None
Return text from selected html element. Return |
get_str(self, selector) [source]
Get string from html element.
Parameters: | selector : str
CSS selector. |
---|---|
Returns: | string : str or None
Return string from selected html element. Return |
copy(self) [source]
Returns: | self : sqlalchemy_mutablesoup.SoupBase
Shallow copy of |
---|
render(self) [source]
Render html for insertion into a Jinja template.
Returns: | rendered : flask.Markup
Rendered html. |
---|
set_element(self, parent_selector, val, target_selector=None, gen_target=None, args=(), kwargs={}) [source]
Set a soup element.
Parameters: | parent_selector : str
CSS selector for the parent of the html element being set (the 'target element'). val : str or bs4.BeautifulSoup
Value to which the target element will be set. If
CSS selector for the target element; a child of the parent element. If
Callable which generates the target element if none of the parent element's children are selected by the
Arguments for
Keyword arguments for |
---|---|
Returns: | self : sqlalchemy_mutablesoup.SoupBase
|
sqlalchemy_mutablesoup.MutableSoup
Mutable BeautifulSoup database object. Inherits from SoupBase
. Note
that a MutableSoup
object can be set using a string or
bs4.BeautifulSoup
object.
Methods
set_element(self, *args, **kwargs) [source]
Inherits from SoupBase.set_element
. The only addition is that this
method also registers that it has changed.
Returns: | self : sqlalchemy_mutable.MutableSoup
|
---|
sqlalchemy_mutablesoup.MutableSoupType
Mutable BeautifulSoup database type associated with MutableSoup
object.