Flask app with ArcGIS, Arcpy does not run
Asked Answered
N

3

38

I have a script that gets a table from MSSQL database and then registers it with ArcGIS. It uses several other arcpy methods as well. I tried to combine it with Flask and developed an HTML interface where you can specify tables. The script runs on console perfectly well, however, when running with Flask on http://127.0.0.1:5000/ , the arcpy functions do not run, then the app throws errors.

I am using my local python directory, so I do not have any problem with importing arcpy on flask. So, I am able to use pymssql functions and create a new table, however when it comes to arcpy function, It throws does not exist error, however, the table exists. I feel like there is something wrong with running arcpy with Flask, but any help would be appreciated.

(2) I tried the same thing in Django but I am having the same problem.

Thanks

forms.py

class createGISLayer(FlaskForm):
    tCreateLayer = SubmitField('Create GIS Layer')

DashboardMain()

   try:
        cursor.execute(QueryCreate)
        print ("Table Created.")
        print(self.dbTablePath)
        descTable = arcpy.Describe(self.dbTablePath)

    except arcpy.ExecuteError:
        print(arcpy.GetMessages())

app.py

if formCreate.tCreateLayer.data and formCreate.validate_on_submit():
    if myLayer is not None:
        try:
            print("Create GIS Layer")
            myLayer.dashboardMain()
            flash('GIS Layer created!', 'success')

        except Exception as e:
            print(e.message)
            flash(e.message, 'danger')

index.html

<!-- Create GIS Layer  -->
<div class="content-section">
<form name='idCreateGISLayer' action="" method="POST">
<table style="height: auto; margin-left: auto; margin-right: auto; width: 600px;">
<tbody>
<tr>
    {{ formCreate.hidden_tag() }}
    <td style="height: 39px; width: 259px">
        <h2 style="text-align: left;"><font size="3"><strong>(2) Create </strong></font></h2>
    </td>
    <td style="text-align: left; height: 39px;">
        <div class="auto-style2">                                                                
            {{ formCreate.tCreateLayer(class="btn btn-outline-info")}}
        </div>
    </td>
 </tr>
 </tbody>
 </table>
 </form>
 </div>

ERROR

Table Created.
F:\Projects\Dashboard\Publish.sde\Publish.dbo.A_WebT1
"F:\Projects\Dashboard\Publish.sde\Publish.dbo.A_WebT1" does not exist

screenshot

Table exists

Nomenclature answered 11/7, 2018 at 19:26 Comment(1)
Can you check if python can access the file using pathlib:from pathlib import Path dbTablePath = ... # list all files in the directory p = Path(dbTablePath) for f in p.iterdir(): print(f) # or for f in p.parent.iterdir(): print(f) Phio
R
1

IIRC, ArcGIS maintains it's own python environment (and used to install it over existing installations, ick). So unless you're using that environment when you launch/run flask, you're likely not getting the config/resources loaded that are needed to support Arcpy functions. It's like trying to load a library installed into a different python virtual environment. You can provide the path, but if it requires any additional content or configuration from it's own venv, it won't work.

Rotate answered 27/4, 2023 at 17:3 Comment(0)
N
-1

Instead of calling the function, I ran my script with DOS.

@app.route('/cad2json', methods=['POST'])
def cad2sde():
    dwg_path = request.form.get('dwg_path')
    reference_scale = request.form.get('reference_scale')
    spatial_reference = request.form.get('spatial_reference')
    target_layer = request.form.get('target_layer')
    sentence =   (u'C:\\Python27\\ArcGIS10.1\\python.exe C:\\Users\\Administrator           \\Desktop\\flask\\cad.py %s %s %s %s'
             %(dwg_path,reference_scale,spatial_reference,target_layer))
    p = os.popen(sentence)
    return format(p.read())
Nevillenevin answered 17/7, 2018 at 10:26 Comment(2)
you need to describe how your answer fits to the requirement at handSorci
In this case, the .py file should be under the same directory of python.exe. And unfortunately, you can not trace any error either. I have been looking for the solution still. There should be something to do with path.Nomenclature
O
-3

ArcPy is not free. It only works in ESRI software or anaconda. You can not import.

Orthoepy answered 13/9, 2021 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.