4 ms·
I'm curious to see some example use cases which are practical. Yes you can embed SQL in Python in HTML, but I think it's pretty obvious that this not a great id
by michaelmior 12y ago
I'm curious to see some example use cases which are practical. Yes you can embed SQL in Python in HTML, but I think it's pretty obvious that this not a great idea. Even more common examples such as embedding CSS in HTML seem fairly ill-advised.
- kot-behemoth 12y agoFirst example that comes to mind is SQL inside Python (I'm actually doing this right now). Say, you've got a raw SQL query in your Django app. To a standard editor, the query is a simple string, however it's absolutely great to be able to mark this string as SQL, getting all the correct syntax highlighting and linting etc. PyCharm already does this, even going a step further, validating the DB schema along the way. Super useful!
- michaelmior 12y agoGood point :)
- radiowave 12y agoDatabase stored procedures almost invariably involve writing Python (or whatever) inside of SQL/DDL. Database change management systems (e.g. Liquibase) will often involve writing chunks of SQL inside of XML/JSON/YAML. More generally, configuration files often involve string parameter values that are actually expressions in some subsystem or related system.
- imanaccount247 12y ago>Database stored procedures almost invariably involve writing Python (or whatever) inside of SQL/DDL What? They can involve that, but it is very variably. I never do that. I find python, perl, etc are much worse than plpgsql.
- radiowave 12y agoSorry, good point. In my head I was thinking "except when using the native procedural language", but it appears I didn't get around to actually typing that bit.
- sklogic 12y agoA typical language mixing example is literate programming: you have your programming language embedded into a markup language (say, latex). Another example is embedded assembly, embedded regular expressions, etc. I also find it quite useful to be able to use embedded Prolog, to embed BNF parsers, etc.
- jarpineh 12y agoOne place would be the Aspen web framework which puts Python code, templates and JSON in a same file with its simplates concept [1]. Each language stays in a separate section so no mixing of logic and presentation, but no reason to separate them either, out of HTTP request/response handling context. An editor supporting this could make it easier to develop web apps with both static HTML and JS enabled browser. Apparently Eco also supports visualization and IPython in some way (according to project changelog)[2]. That is very interesting, actually. [1] http://aspen.io/simplates/ http://aspen.io/simplates/ [2] https://bitbucket.org/softdevteam/eco https://bitbucket.org/softdevteam/eco
- Erwin 12y agoWell, you would put SQL inside a string in Python. PyCharm/IntelliJ already recognize that: as I write execute("SELECT... ") it considers the string PostgreSQL and autocompletes among the table names in my project's database; and If I write ("SELECT foo FROM bar") if the bar table has no foo, it highlights foo just like it would a Python variable that was not defined. Now interestingly when you are within that SQL string you can press Alt-Enter (the general key for some context sensitive magic) and open a new nested editor window with just the PostgreSQL fragment. Let's say that PostgreSQL fragment now itself embeds Python code again (now running server-side using PL/Python). Yep, you can tell it the string inside SQL is Python.. and open another window. I then added an XML string inside the Python inside SQL inside Python, however it wouldn't go any deeper. However it does give you the syntax highlighting and language-specific warnings in XML, PostgreSQL, Python within Postgresql and top level Python: Beyond the first level however, I had to manually tell it that this string is Python.
- skybrian 12y agoI think it solves a practical problem for HTML templates. For example, React embeds HTML templates into JavaScript using JSX. Unlike most template languages that generate strings, their templates build a JavaScript tree structure without an intermediate string phase. This is rather hard to do in a practical way without a language extension, since having a separate file for every tiny HTML template is unwieldy. Also, most alternatives involve building a shadow language [1] into the template language which isn't as elegant as writing code in whichever programming language you're already using. [1] http://gbracha.blogspot.com/2014/09/a-domain-of-shadows.html http://gbracha.blogspot.com/2014/09/a-domain-of-shadows.html