{ "info": { "author": "Jonathan Goodnow", "author_email": "jon@goodnow.io", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Topic :: Utilities" ], "description": "django-mathfield\r\n================\r\n\r\nMathField is a model field for Django that allows you to input LaTeX and store \r\nthe compiled HTML on your database. It comes with a form for the Django Admin \r\nthat provides live previews of your rendered LaTeX.\r\n\r\nInstallation and Setup\r\n----------------------\r\n\r\nYour server needs to have \r\n`Python 2.7 `_ and \r\n`Django 1.7 `_.\r\n\r\nGet it installed with::\r\n\r\n $ pip install django-mathfield\r\n\r\nAdd :code:`'mathfield'` to your :code:`INSTALLED_APPS` in your Django project's\r\n:code:`settings.py`.\r\n\r\nAdd a :code:`MathField` to one of your models like this:\r\n\r\n.. code:: python\r\n\r\n from django.db import models\r\n import mathfield\r\n\r\n class Lesson(models.Model):\r\n lesson_plan = mathfield.MathField()\r\n\r\nGet live previews of the rendered LaTeX while you're editing in the Django admin\r\nby adding :code:`MathFieldWidget` as a widget when registering your model in\r\n:code:`admin.py`:\r\n\r\n.. code:: python\r\n\r\n from django.contrib import admin\r\n from django import forms\r\n from yourapp.models import Lesson\r\n import mathfield\r\n\r\n class LessonAdminForm(forms.ModelForm):\r\n\r\n class Meta:\r\n widgets = {\r\n 'lesson_plan': mathfield.MathFieldWidget\r\n }\r\n\r\n\r\n class LessonAdmin(admin.ModelAdmin):\r\n form = LessonAdminForm\r\n\r\n\r\n admin.site.register(Lesson, LessonAdmin)\r\n\r\nAfter adding some data to your database, you can output the rendered HTML to\r\na template:\r\n\r\n.. code:: html\r\n\r\n \r\n \r\n \r\n {% load staticfiles %}\r\n \r\n \r\n \r\n
\r\n Raw text/LaTeX: {{ lesson.lesson_plan.raw }}\r\n
\r\n
\r\n Rendered HTML: {{ lesson.lesson_plan.html|safe }}\r\n
\r\n \r\n \r\n\r\nMake sure that you include the :code:`mathfield.css` stylesheet in your template\r\nhead, and include :code:`|safe` with the MathField HTML value. This will\r\ngive Django permission to render the text in that field as HTML. It is safe to\r\ndo this provided that you only update the HTML using the form in the Django\r\nadmin or the functions provided in the MathField API. Be very careful when\r\nupdating the HTML yourself!\r\n\r\nDeveloper API\r\n-------------\r\n\r\nYou can modify MathField data and compile LaTeX on your server without the admin\r\nform if you would like. To be able to compile LaTeX serverside, you must have\r\n`node.js `_ (v0.10+) installed and it must be on \r\nyour system path as an executable called :code:`node`. Note that this is not\r\nnecessary if you just use the admin form, as all compilation will occur in the\r\nbrowser in this case.\r\n\r\nInstantiating Models\r\n********************\r\n\r\nThere are two ways to pass data to a MathField: as a string, or as a dictionary\r\nwith the keys :code:`raw` and :code:`html`. If you pass a string, the html will\r\nbe rendered for you.\r\n\r\nLet's say you are using the :code:`Lesson` model from above, which has a\r\n:code:`lesson_plan` column that is a MathField. You can create a new instance\r\nwith:\r\n\r\n.. code:: python\r\n \r\n new_lesson = Lesson(lesson_plan='One half is $\\\\frac{1}{2}$.')\r\n new_lesson.save()\r\n\r\nYou can also pass a dictionary that contains the raw text under the key\r\n:code:`raw` and the already rendered HTML under the key :code:`html`. This is\r\nparticularly useful if you want to generate the HTML yourself, perhaps because\r\nyou can't install node.js on your server, or because you want to use a typesetting\r\nlibrary other than `KaTeX `_.\r\n\r\nThe function :code:`store_math` provided in the mathfield API is provided for\r\nconvenience. If you don't know the HTML, you don't have to provide it, and it\r\nwill be generated for you. Otherwise, you can pass in the HTML and it will just\r\nuse that. For example:\r\n\r\n.. code:: python\r\n\r\n import mathfield\r\n\r\n # if you already know the HTML:\r\n math_data = mathfield.store_math(raw_text, html)\r\n\r\n # if you don't:\r\n math_data = mathfield.store_math(raw_text)\r\n\r\n new_lesson = Lesson(lesson_plan=math_data)\r\n new_lesson.save()\r\n\r\nDatabase Lookups\r\n****************\r\n\r\nWhen you look up an existing MathField, you get a dictionary with the keys\r\n:code:`raw` and :code:`html`:\r\n\r\n.. code:: python\r\n\r\n lesson = Lesson.objects.get(id=0)\r\n print lesson.lesson_plan['raw']\r\n # One half is $\\frac{1}{2}$\r\n\r\n print lesson.lesson_plan['html']\r\n # the html for your template...\r\n\r\nJust Getting Some HTML\r\n**********************\r\n\r\nIf you just want to pass in a string and get the HTML, use \r\n:code:`render_to_html`:\r\n\r\n.. code:: python\r\n\r\n import mathfield\r\n\r\n html = mathfield.render_to_html('One half is $\\\\frac{1}{2}$.')", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/jongoodnow/django-mathfield", "keywords": "django,math,latex,katex", "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "django-mathfield", "package_url": "https://pypi.org/project/django-mathfield/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-mathfield/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/jongoodnow/django-mathfield" }, "release_url": "https://pypi.org/project/django-mathfield/0.1.0/", "requires_dist": null, "requires_python": null, "summary": "A Django model field for writing and displaying LaTeX", "version": "0.1.0" }, "last_serial": 1315423, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "d5e36379c97f8dd11f4ee47a7dd4344c", "sha256": "c60209b4dd5ef38921c4ab0b6c407376be7764c26a54ec4ba7d83f50274e8f4f" }, "downloads": -1, "filename": "django-mathfield-0.1.0.tar.gz", "has_sig": false, "md5_digest": "d5e36379c97f8dd11f4ee47a7dd4344c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 602534, "upload_time": "2014-11-21T03:35:30", "url": "https://files.pythonhosted.org/packages/d0/bc/ee3ad5fc368cbd719cde32912d9a9eb335c3affdf87fc8b9fb55e941fd45/django-mathfield-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "d5e36379c97f8dd11f4ee47a7dd4344c", "sha256": "c60209b4dd5ef38921c4ab0b6c407376be7764c26a54ec4ba7d83f50274e8f4f" }, "downloads": -1, "filename": "django-mathfield-0.1.0.tar.gz", "has_sig": false, "md5_digest": "d5e36379c97f8dd11f4ee47a7dd4344c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 602534, "upload_time": "2014-11-21T03:35:30", "url": "https://files.pythonhosted.org/packages/d0/bc/ee3ad5fc368cbd719cde32912d9a9eb335c3affdf87fc8b9fb55e941fd45/django-mathfield-0.1.0.tar.gz" } ] }