Store Python code in Postgres as text using Laravel/PHP
Get the solution ↓↓↓I am working on an application that should receive some string that corresponds to Python code. I need to store this code to a Postgres database using Laravel and then be able to read it back to show it into a editor text box within a webapp. I have two problems:
- At some point this code was received and stored as a Python file (file.py), so I have a bunch of .py files I need to read and return as a text.
- I need some means to read these files and store its text in a Postgres database using Laravel.
So far I manage to read the files like this:
$code_str = null;
if(Storage::disk('local')->exists($ci->code->code)){
$code_str = Storage::disk('local')->get($ci->code->code);
}
return response()->json(['status' => true, 'data' => compact('code_str')]);
However I read the Python code like this:
{
"status": true,
"data": {
"code_str": "import matplotlib.pyplot as plt\nimport numpy as np\n\n# https://python4astronomers.github.io/plotting/advanced.html\n\ndef plot1(ax, x):\n ax.plot(x, x ** 1.5, 'ro', label='density')\n\n\ndef plot2(ax, x):\n ax.plot(x, 20 / x, 'bx', label='temperature')\n\ndef plot3(ax, x):\n\n\nfig = plt.figure()\nax = fig.add_subplot(1, 1, 1)\n\nx = np.linspace(1., 8., 30)\n\nplot1(ax, x)\nplot2(ax, x)\n\nplt.savefig('test.png')\n"
}
}
At the webapp those \n are causing me problems.
So my questions are how can I return the Python code to the front end preserving the line breaks and spaces (since those are critical to Python) and how can I safely store and retrieve Python code from a Postgres database.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illuminate\http\exceptions\posttoolargeexception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.