{ "cells": [ { "cell_type": "markdown", "id": "4baa72c7-b196-4658-b8bb-b8aaf73fd631", "metadata": {}, "source": [ "https://hub.xpub.nl/soupboat/~carmen/api/" ] }, { "cell_type": "code", "execution_count": null, "id": "45f690d6-66c4-4cb9-ad9b-cbac2def7acf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * Serving Flask app '__main__' (lazy loading)\n", " * Environment: production\n", "\u001b[31m WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n", "\u001b[2m Use a production WSGI server instead.\u001b[0m\n", " * Debug mode: off\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " * Running on http://127.0.0.1:9086/ (Press CTRL+C to quit)\n", "127.0.0.1 - - [26/Nov/2021 12:24:27] \"GET /~carmen/api/ HTTP/1.0\" 200 -\n", "127.0.0.1 - - [26/Nov/2021 12:24:27] \"\u001b[33mGET /~carmen/api/style.css HTTP/1.0\u001b[0m\" 404 -\n" ] } ], "source": [ "from flask import Flask, request, json\n", "from weasyprint import HTML, CSS\n", "\n", "app = Flask(__name__)\n", "\n", "\n", "def swap(text, target, replacement):\n", " result= text.replace(target, replacement)\n", " return result\n", "\n", "#In order to make the function work in an URL I need to run the flask code, 1st thing change \n", "#\"user\" for Carmen this is the main route: @app.route(f\"/~{USER}/api/\") and it has to have your\n", "#username and take out the brackets and also change the \"PORT\" for my port number which is \n", "# 9086\n", "#Flask is a kind of manager that connects our scripts in Python with the web. So we are using flask\n", "#flask to make our function appear in a URL\n", "#Asterisk means that is running\n", "#Press stop and run it \n", "\n", "@app.route(f\"/~carmen/api/\")\n", "def replace():\n", " \n", " text=request.values.get('text', '')\n", " target = request.values.get('target', '')\n", " replacement = request.values.get('replacement', '')\n", " title = f\"

{request.values.get('title', '')}

\"\n", " txt_result = f\"

{swap(text, target, replacement)}

\"\n", " contents= title + txt_result\n", " \n", " with open(\"db.txt\", \"a\") as db:\n", " db.write(contents+ \" \\n\")\n", " \n", " with open (\"archive.html\", \"a\") as output:\n", " print(contents, file=output)\n", " HTML(filename=\"archive.html\").write_pdf('./replace_booklet.pdf')\n", " \n", " return f\"\"\"\n", " \n", " \n", "\n", " \n", " \n", " \n", " Untitled\n", " \n", " \n", " \n", " go to archive deheh --> \n", "
\n", " \n", " \n", " \n", " \n", "\n", " \n", "
\n", " \n", "\n", "
\n", "
{text}
\n", "
{contents}
\n", "
\n", " \n", " \n", "\n", " \n", " \n", " \"\"\"\n", "@app.route(f\"/~grgr/api/archive/\", methods=['GET'])\n", "def archive():\n", " \n", " archive= open(\"archive.html\").read()\n", " \n", " return f\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " \n", " archive\n", " \n", " \n", " \n", "

Archive of transformed texts

\n", " {archive}\n", " \n", " \n", " \n", " \n", " \"\"\"\n", "\n", "\n", "app.run(port=9086)" ] }, { "cell_type": "code", "execution_count": 3, "id": "64237da7-4bad-4fb6-9916-1105d10e3e26", "metadata": {}, "outputs": [], "source": [ "sent= \"prova\" \n", "\n", "# with open(\"db.txt\", \"a\") as db:\n", "# db.write(sent)" ] }, { "cell_type": "code", "execution_count": 5, "id": "540e00da-7e8e-44cf-8b09-7d793e8892c7", "metadata": {}, "outputs": [], "source": [ "from weasyprint import HTML, CSS\n", "\n", "# with is nice cause it auto-closes the file once \"outside\" the with block\n", "with open (\"test.html\", \"a\") as output:\n", " print (\"
\", file=output)\n",
    "    print (sent + \" \", file=output)\n",
    "    print (\"
\", file=output)\n", "HTML(filename=\"test.html\").write_pdf('./test.pdf')" ] }, { "cell_type": "code", "execution_count": 41, "id": "db104bf9-1283-4ea9-ae6b-2fa361a5de6b", "metadata": {}, "outputs": [], "source": [ "\n", " \n", "page= open(\"archive.html\").read()" ] }, { "cell_type": "code", "execution_count": 42, "id": "db85d308-10f7-4b95-9936-60638bc2e9fe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"

bacon test

this m🥓rning I tried t🥓 print a pdf directly fr🥓m this f🥓r , let's see if it w🥓rks n🥓w

\\n

secOOOnd test

what happens if nOOOOOw i write anOOOOOther text??

\\n

N*ICE

nO*O*O*O*O*O*O*Oice

\\n

N*ICE

nO*O*O*O*O*O*O*Oice

\\n

N*ICE AGAIN

nO*O*O*O*O*O*O*Oice againnnnn

\\n\\n\"" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "page" ] }, { "cell_type": "code", "execution_count": null, "id": "858662cb-43f0-48c4-9221-80faa22ece3c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 5 }