Ejemplo n.º 1
0
def as_html_handle_data_algebra(mathobj):
    """Add some logic to allow the client to provide the math object in several ways:

    - If it is a mathobject, it will be turnied into LaTeX and wrapped in the mathjax escape token.
    - If it is a container of mathobjects, then each one will be turned into LaTeX and presented on
      its own line.
    - If it is not a mathobject then the str function will be invoked.

    :param mathobj: The data algebra construct to be rendered on HTML
    :return: The HTML snippet of the mathobject.
    """

    if isinstance(mathobj, _mo.MathObject):
        # print this one math object
        return "$$" + _html.escape(_math_object_to_latex(mathobj)) + "$$"
    elif isinstance(mathobj, _collections.Iterable):
        temp = ""
        for elem in mathobj:
            if isinstance(elem, _mo.MathObject):
                # latex
                temp += "$$\(\\require{color}\)\(\\require{xcolor}\)" + \
                        _html.escape(_math_object_to_latex(elem)) + "$$ <br //>"
            else:
                # str
                temp += _html.escape(str(elem)) + "<br //>"

        return temp
    else:
        # print this one non-math object using str(mathobjects)
        return _html.escape(str(mathobj))
Ejemplo n.º 2
0
def create_simple_web_page(mathobj: _mo.MathObject) -> str:
    """Convert a math object into an HTML file.  This does not offer much client specification or
    HTML injection.  Create a basic webpage for one mathobject.

    :param mathobj: The mathobject to create a webpage for.
    :return: The web page (HTML) for ``mathobj``.
    """
    web_template = """\
<html>
    <head>
        <script
        src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'>
        </script>
        <script type="text/javascript">
        //<![CDATA[  //]]>
        </script>
        <style>
            body {{background-color: #F8F8FF}}
            #latexArea, #dataAlgebraArea{{
                width: 1000px;
                height: 200px;
                border: 1px solid black;
                padding: 5px;
                overflow: scroll;
                background-color: white;
                resize: both;
            }}
        </style>
    </head>
    <body>
        <h1>MathJax is Easy!</h1>
        <h2>Input:</h2>
        <div id="dataAlgebraArea">{data_algebra_in!s}</div>
        <h2>Output:</h2>
        <div id="latexArea">$$\(\\require{color}\){latex_out}$$</div>
    </body></html>
    """

    web_out = web_template.format(data_algebra_in=mathobj, latex_out=_math_object_to_latex(mathobj))
    print(web_out)
    return web_out
Ejemplo n.º 3
0
file = StringIO(sales_csv)
sales_multiclan = import_csv(file, has_dup_rows=True)  # note the use of flag to return a multiclan
print(sales_multiclan)

# lets see how our cashiers are doing
cashier_diagonal = _mo.Multiset({_mo.Set(_mo.Couplet('cashier', 'cashier')): 1})
cashier_sales = _multiclans.compose(sales_multiclan, cashier_diagonal)
print("To find out how many sales each cashier has had, we can do the following:")
print(str("sales_multiclan") + ' COMPOSE ' + str(cashier_diagonal))
print('=> EVALUATES TO ' + str(cashier_sales) + "\n")

# lets see what products we sold for the day
product_diagonal = _mo.Multiset({_mo.Set(_mo.Couplet('product', 'product')): 1})
product_sales = _multiclans.compose(sales_multiclan, product_diagonal)
print("To find out how many products we sold, we can do the following:")
print(str("sales_multiclan") + ' COMPOSE ' + str(product_diagonal))
print('=> EVALUATES TO ' + str(product_sales) + "\n")

# Print some of these new math objects in LaTeX.
print(_math_object_to_latex(simple_union))
print(_math_object_to_latex(sales_multiclan))
print(_math_object_to_latex(product_sales))

# Export a multiclan
print("\nConverting the multiclan back to a csv file is easy.")
sales_csv_out = StringIO()
export_csv(sales_multiclan, sales_csv_out)
csv_str = sales_csv_out.getvalue()
print(csv_str)
Ejemplo n.º 4
0
# lets see how our cashiers are doing
cashier_diagonal = _mo.Multiset(
    {_mo.Set(_mo.Couplet('cashier', 'cashier')): 1})
cashier_sales = _multiclans.compose(sales_multiclan, cashier_diagonal)
print(
    "To find out how many sales each cashier has had, we can do the following:"
)
print(str("sales_multiclan") + ' COMPOSE ' + str(cashier_diagonal))
print('=> EVALUATES TO ' + str(cashier_sales) + "\n")

# lets see what products we sold for the day
product_diagonal = _mo.Multiset(
    {_mo.Set(_mo.Couplet('product', 'product')): 1})
product_sales = _multiclans.compose(sales_multiclan, product_diagonal)
print("To find out how many products we sold, we can do the following:")
print(str("sales_multiclan") + ' COMPOSE ' + str(product_diagonal))
print('=> EVALUATES TO ' + str(product_sales) + "\n")

# Print some of these new math objects in LaTeX.
print(_math_object_to_latex(simple_union))
print(_math_object_to_latex(sales_multiclan))
print(_math_object_to_latex(product_sales))

# Export a multiclan
print("\nConverting the multiclan back to a csv file is easy.")
sales_csv_out = StringIO()
export_csv(sales_multiclan, sales_csv_out)
csv_str = sales_csv_out.getvalue()
print(csv_str)