Adding Flask support to an existing Pycharm project - python

I'm working on a small project where it would be helpful to display trivial web pages. It's being developed in PyCharm, the pro edition.
This is an existing project, so it wasn't created as a Flask app to begin with.
I've added Flask to my requirements.txt and ensured that its installed. However, I don't get autocompletion of any Jinja templates - essentially, the HTML file I created is treated like dumb HTML.
How do I get PyCharm to recognize that I'm using Flask and to act accordingly?

You have to tell PyCharm which folders contain your templates.
Go to Settings/Project: NAME/Project Structure
Click on the folder containing your templates and click the purple Templates button.
It will probably warn you that no template language is specified. If that is the case
go to Settings/Language & Frameworks/Python Template Languages
In the combobox Template Language: select your template language (jinja?)
Under Template file types select the file type (probably HTML)

When you create a project ( File->New Project ) to choose:
After the creation of the project to select the appropriate file:

Creating a new Flask Project will just set the python template language as Jinja2 and give you a skeleton (a web server which respond 'Hello World' to client).
Therefore, if you have a existing project, press Prefences -> Search Python Template Languages -> and now you can change it to Jinja2

Related

Python Django: Django won't render images from a folder outside static

Been working on a Django project for a personal portfolio. Inside the project I have different apps: a blog app and a projects app. When I try to render a images it wont display unless a use a specific path from the templates folder to the static folder. For example, based on my project directory, if I use "../static/img/example.jpg" it renders the image with out problem, but as soon as I use another path like "../../media/img/example.jpg" the image wont render and a blank space will appear. I would like to know is this a normal behavior with Django and if it is, then what is the best practice to display images, because nothing comes to mind right now.enter image description here
Django delivers files from static folder or the path we specified in STATIC_FILE_DIRS in settings. Media folder is intended to store the User uploaded files, not the static files. Again we have to set those in settings. This is the Django default behaviour.
You can use any cloud storage like AWS S3 to store files.

Live templates file does not exist in pycharm templates folder?

I wanted to insert multiple live templates. For example whenever I type prop and press tab, then pycharm would write the full property for python.
I wanted to import the following list:
https://github.com/hoffmann/PyCharm-Python-Templates
But I can not find the templates folder in <your home directory>\.<product name><version number>\config\templates, as said in https://www.jetbrains.com/help/pycharm/2016.1/live-templates.html
Also I am using the community edition 2016.1.4.
I got this to work on Windows when I used the path <your home directory>\.PyCharm2018.3\config\jba_config\templates\user.xml (my version of PyCharm is 2018.3.4)
In case this doesn't work for you, you may find where the file belong by following what I did.
Open File | Settings | Editor | Live Templates in PyCharm, created a new template called MyNewUniqueLiveTemplate
On the command line in the directory <user directory>\.PyCharm2018.3\config "grep"ed for the string MyNewUniqueLiveTemplate.
I've had the same issue and found that there is one simplest way to insert multiple live templates.
Just copy desired template code from *.XML file,
open PyCharm's Live Templates Settings (Ctrl+Alt+S -> Editor / Live Templates),
select Template Group (such as Python or create your own like a 'user'),
and paste in the value. That's all!
It works fine dependless of the operating system, PyCharm version and you shouldn't worry about PyCharm config files location.
You can insert in that way multiply live templates at once.

Bottle Template Support?

I'm using PyCharm 3.4.1 and learning MongoDB from Mongo University. In the code, they have us using Python to create html pages using the MVC pattern with bottle.
When I add a file of type .tpl to the editor in PyCharm, there is no "intellisense" or support for the model in the code. Changing it to be in the list of html file types helps some, but no python support in the editor.
Is there a plugin or some other change i can make to support editing better? Seems someone else asked something similar earlier in the year at How to get tpl files highlighted in pycharm?
Bottle comes with a built-in template engine called SimpleTemplate which is not currently supported by PyCharm.
There is a relevant feature request: Bottle microframework support, but it doesn't explicitly state about .tpl syntax support.
I'd go and create a feature request in PyCharm's issue tracker asking about SimpleTemplate syntax support or, at least, vote up and comment in the existing issue.
For example, there is the similar feature-request for tornado template engine:
Add Tornado Template Support
It has currently 56 votes up which is probably increasing the possibility of seeing it happen.
As a workaround, you can configure .tpl file type under Settings -> File Types. It is possible to set highlighting rules there.

PyCharm3 Live template not working at all

I just switched from Eclipse/PyDev to PyCharm 3 CE. I can't get the live templates working any ways. Followed every documentation I can find online like this:
http://www.jetbrains.com/pycharm/webhelp/live-templates.html
And the builtin python template super(Generates a 'super' call) does not work either.
By "not working", I meant that when editing a python file in PyCharm and typing the abbreviation, the popup snippets list does not include the live templates, even though they are under the Python template group.
Am I missing something?
I am using PyCharm 3.1 Professional (evaluation version) and am having the same issue.
However, using Ctrl+J will bring up templates as will Code -> Insert Live Template. It is also possible to start typing a code snippet and then press Ctrl+J to see a filtered list of templates.
Don't forget to also select the "context"...

Python AppEngine And HTML

I have a simple Hello World application in Python that i'm using with AppEngine, but i want to insert this in a HTML file, like this: I have a file called test.html and on it i have this snippet:
<center><img src="test.png></center>
// Here comes the Python App //
I want to put the output of the Python application in this space, like i can do with Servlets(Java).
Regards.
You should work through the getting started docs at the AppEngine site. Specifically around templates:
http://code.google.com/appengine/docs/python/gettingstarted/templates.html.
Use the Django templates - the module comes built in with the app engine. I've used it here on line 42. The template used is here.
I would really recommend using a boilerplate template
https://github.com/coto/gae-boilerplate
This would give you the layout and template structure for developing useful and professional sites

Categories

Resources