I'm trying to create new instance of [arcgis.features.FeatureLayer](https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#featurelayer) using python arcgis package.
I find it impossible, I'm traversing through [documentation](https://developers.arcgis.com/python/api-reference) and [examples](https://developers.arcgis.com/python/guide/appending-features/#Append-attribute-values-from-the-csv-item-into-target-feature-layer-attribute-field) trying different things and I find myself going round in circles and slowly I start pulling my hear out...
This is what I have:
<SPAN class="keyword token">import</SPAN> arcgis
<SPAN class="keyword token">import</SPAN> json
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS
g <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>username<SPAN class="operator token">=</SPAN><SPAN class="string token">"my_uname"</SPAN><SPAN class="punctuation token">,</SPAN> password<SPAN class="operator token">=</SPAN><SPAN class="string token">"my_pwd"</SPAN><SPAN class="punctuation token">)</SPAN>
prop <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">.</SPAN>FeatureCollection<SPAN class="punctuation token">(</SPAN>dictdata<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">'attributes'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'foo'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'bar'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'lorem'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'ipsum'</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
prop_json<SPAN class="operator token">=</SPAN>json<SPAN class="punctuation token">.</SPAN>dumps<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"featureCollection"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"layers"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>dict<SPAN class="punctuation token">(</SPAN>prop<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
item_properties<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"type"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Feature Collection"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"title"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"test_feature_collection_01"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"text"</SPAN><SPAN class="punctuation token">:</SPAN> prop_json<SPAN class="punctuation token">}</SPAN>
it <SPAN class="operator token">=</SPAN> g<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>item_properties<SPAN class="operator token">=</SPAN>item_properties<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>At this point - I can't understand why `it.layers` yields empty results. I believe `item_properties` is malformed resulting in `arcgis` ignoring my layers definition... but I have nowhere to check what it should look like. I figured I'd like to use something from `arcgis` to generate layer definition for me rather than to handcraft JSON myself so it's future-proof.
What I want to do with that item later is this:
lr <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">.</SPAN>FeatureLayer<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>item<SPAN class="operator token">=</SPAN>it<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
This fails with `TypeError: item must be a type of service, not Feature Collection`
So I figured I could publish item and use it here.
pit <SPAN class="operator token">=</SPAN> it<SPAN class="punctuation token">.</SPAN>publish<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
lr <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">.</SPAN>FeatureLayer<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>item<SPAN class="operator token">=</SPAN>pit<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I need `FeatureLayer` to be able to call `append` (so I can just throw extra data each time I have anything new I want to push)
But to my surprise I cannot even publish the item and I'm getting `Exception: Job failed.` (to my bigger surprise item actually gets published as I can see it through content manager website)
I also tried creating "CSV" item type:
<SPAN class="keyword token">import</SPAN> arcgis
<SPAN class="keyword token">import</SPAN> json
<SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS
g <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN>username<SPAN class="operator token">=</SPAN><SPAN class="string token">"my_uname"</SPAN><SPAN class="punctuation token">,</SPAN> password<SPAN class="operator token">=</SPAN><SPAN class="string token">"my_pwd"</SPAN><SPAN class="punctuation token">)</SPAN>
item_properties<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"type"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"CSV"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"title"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"test_feature_collection_01"</SPAN><SPAN class="punctuation token">}</SPAN>
it <SPAN class="operator token">=</SPAN> g<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>item_properties<SPAN class="operator token">=</SPAN>item_properties<SPAN class="punctuation token">,</SPAN> data<SPAN class="operator token">=</SPAN><SPAN class="string token">"/tmp/foo.csv"</SPAN><SPAN class="punctuation token">)</SPAN>
pit <SPAN class="operator token">=</SPAN> it<SPAN class="punctuation token">.</SPAN>publish<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
lr <SPAN class="operator token">=</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features<SPAN class="punctuation token">.</SPAN>FeatureLayer<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>item<SPAN class="operator token">=</SPAN>pit<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>however this results in layer-less item, which results in unhandled exception `IndexError: list index out of range` (because method `arcgis` tries to get to layers which is empty...)
Please help...