def select(self, query, from_content=False):
        el = self.element
        if from_content:
            el = el.content

        _el = el.querySelector(query)
        if _el:
            return Element(_el.id, _el)
        else:
            console.log(f"WARNING: can't find element matching query {query}")
Beispiel #2
0
 async def add_ui(self, source_id, ui):
     # print("Handling new user...")
     # Might as well make sure that we don't have other charts that are out of date.
     # So let's send an update to the existing charts first.
     await self.update_a()
     self.ui_tabs[source_id] = ui
     # "initialize" command sets chart range and page in addition to setting the chart.
     # "initialize" does a superset of what "reset" does.
     try:
         await ui.initializeSseq(js_JSON.parse(JSON.stringify(self.chart)))
     except Exception as e:
         console.log(e)
Beispiel #3
0
 def daily_nutrients(self, *args):
     all_nutrients = {}
     for day in self.state.plan.days:
         for (meal, foods) in day:
             for food in foods:
                 for nutrient in nutrient_names:
                     stored_name = nutrient_names_lookup.get(nutrient)
                     console.log(food.nutrients_per_serving)
                     if hasattr(food.nutrients_per_serving, stored_name):
                         all_nutrients[nutrient] = all_nutrients.get(
                             nutrient,
                             0.0) + food.nutrients_per_serving[stored_name]
     for nutrient in all_nutrients:
         all_nutrients[nutrient] = all_nutrients[nutrient] / float(
             self.state.plan.days.length)
     return all_nutrients
    def write(element_id, value, append=False, exec_id=0):
        """Writes value to the element with id "element_id"""
        console.log(f"APPENDING: {append} ==> {element_id} --> {value}")
        if append:
            child = document.createElement('div');
            element = document.querySelector(f'#{element_id}');
            if not element:
                return
            exec_id = exec_id or element.childElementCount + 1
            element_id = child.id = f"{element_id}-{exec_id}";
            element.appendChild(child);

        element = document.getElementById(element_id)
        html, mime_type = format_mime(value)
        if mime_type in ('application/javascript', 'text/html'):
            scriptEl = document.createRange().createContextualFragment(html)
            element.appendChild(scriptEl)
        else:
            element.innerHTML = html
Beispiel #5
0
	def emit(self, record: logging.LogRecord) -> None:
		msg = self.format(record)
		if record.levelno == logging.DEBUG:
			console.debug(msg, *record.args)
		elif record.levelno == logging.INFO:
			console.info(msg, *record.args)
		elif record.levelno == logging.WARNING:
			console.warn(msg, *record.args)
		elif record.levelno == logging.ERROR:
			if record.exc_info:
				console.error(msg, *record.args)
			else:
				myargs = ["color: red; font-weight: bold;"]
				myargs.extend(record.args)
				msg = "%c{0}".format(msg)
				console.log(msg, *myargs)
		elif record.levelno == logging.CRITICAL:
			console.error(msg, *record.args)
		else:
			console.log("dont know which level", record.msg, *record.args)
Beispiel #6
0
 async def reset_caches(self):
     confirm = await PromiseProxy(self['$ons'].notification.confirm('Reset caches?'))
     if confirm:
         console.log('removing serviceworker registrations')
         registrations = await PromiseProxy(window.navigator.serviceWorker.getRegistrations())
         for registration in registrations:
             await PromiseProxy(registration.unregister())
         console.log('removing foods DB')
         await PromiseProxy(foods_db.destroy())
         console.log('removing window caches')
         await PromiseProxy(window.caches.delete('nutrition'))
         console.log('reloading')
         window.location.reload()
Beispiel #7
0
    def get_nutrition_facts(self, *args):
        console.log('calculating nutrition facts')
        all_nutrients = {}
        for ingredient in self.state.recipe.ingredients:
            # calculate the number of grams this particular ingredient takes up
            amount = ingredient.amount or 0
            if ingredient.portion is None or ingredient.amount is None:
                continue
            portion = ingredient.portion
            portion_record = list(
                filter(
                    lambda portion_record: portion_record.modifier == portion,
                    ingredient.ingredient.portions))[0]
            servings_ratio = (1.0 / float(self.state.recipe.servings))
            portion_ratio = (portion_record.weight_grams /
                             100.0) * float(amount)
            ratio = servings_ratio * portion_ratio
            for (nutrient, value) in ingredient.ingredient.nutrients_per_100g:
                effective_value = ratio * value
                if nutrient not in all_nutrients:
                    all_nutrients[nutrient] = 0.0
                all_nutrients[nutrient] += effective_value

        return all_nutrients
Beispiel #8
0
 async def send_batched_messages_a(self, messages):
     console.log("Sending batched messages:", messages)
     await self.update_charts_a(messages=messages)
     await self.maybe_autosave()
Beispiel #9
0
	def onDragLeave(self, event):
		console.log("onDragLeave", event)
		event.stopPropagation()
		event.preventDefault()
Beispiel #10
0
	def onDragOver(self, event):
		console.log("onDragEnter", event)
		event.stopPropagation()
		event.preventDefault()
Beispiel #11
0
 def write(self, value, append=False):
     console.log(f"Element.write: {value} --> {append}")
     # TODO: it should be the opposite... pyscript.write should use the Element.write
     #       so we can consolidate on how we write depending on the element type
     pyscript.write(self._id, value, append=append)
Beispiel #12
0
 def on_lists_loaded(error, result=None):
     if error:
         console.log('error', error)
     list_store.load_list()
Beispiel #13
0
 def on_plans_loaded(error, result=None):
     if error:
         console.log('error', error)
     meal_plans_store.load_plans()
Beispiel #14
0
 def on_recipes_loaded(error, result=None):
     if error:
         console.log('error', error)
     store.load_db()